swift 中引入外部類C

來源:五

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#/apple_ref/doc/uid/TP40014216-CH10-XID_75




Importing External Frameworks

You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to Yes.

You can import a framework into any Swift file within a different target using the following syntax:

SWIFT

  • import FrameworkName

You can import a framework into any Objective-C .m file within a different target using the following syntax:

OBJECTIVE-C

  • @import FrameworkName;
 

Import into Swift

Import into Objective-C

Any language framework

import FrameworkName

@import FrameworkName;

Using Swift from Objective-C

Once you import your Swift code into Objective-C, use regular Objective-C syntax for working with Swift classes.

OBJECTIVE-C

  • MySwiftClass *swiftObject = [[MySwiftClass alloc] init];
  • [swiftObject swiftMethod];

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you. For more information, see Swift Type Compatibility.

You’ll have access to anything within a class or protocol that’s marked with the @objc attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:

  • Generics

  • Tuples

  • Enumerations defined in Swift

  • Structures defined in Swift

  • Top-level functions defined in Swift

  • Global variables defined in Swift

  • Typealiases defined in Swift

  • Swift-style variadics

  • Nested types

  • Curried functions

For example, a method that takes a generic type as an argument or returns a tuple will not be usable from Objective-C.

To avoid cyclical references, don’t import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. However, note that you cannot subclass a Swift class in Objective-C.

To reference a Swift class in an Objective-C header file

  • Forward declare the Swift class you’re using:

    OBJECTIVE-C

    • // MyObjcClass.h
    • @class MySwiftClass;
    • @interface MyObjcClass : NSObject
    • - (MySwiftClass *)returnSwiftObject;
    • /* ... */
    • @end

Naming Your Product Module

The name of the Xcode-generated header for Swift code, and the name of the Objective-C bridging header that Xcode creates for you, are generated from your product module name. By default, your product module name is the same as your product name. However, if your product name has any nonalphanumeric characters, such as a period (.), they are replaced with an underscore (_) in your product module name. If the name begins with a number, the first number is replaced with an underscore.

You can also provide a custom name for the product module name, and Xcode will use this when naming the bridging and generated headers. To do this, change the Product Module Name build setting.

Troubleshooting Tips and Reminders

  • Treat your Swift and Objective-C files as the same collection of code, and watch out for naming collisions.

  • If you’re working with frameworks, make sure the Defines Module build setting under Packaging is set to Yes.

  • If you’re working with the Objective-C bridging header, make sure the Objective-C Bridging Header build setting under Swift Compiler - Code Generation has a path to the header that’s relative to your project. The path must be directly to the file itself, not just to the directory that it’s in.

  • Xcode uses your product module name—not your target name—when naming the Objective-C bridging header and the generated header for your Swift code. For information on product module naming, seeNaming Your Product Module.

  • To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.

  • When you bring Swift code into Objective-C, remember that Objective-C won’t be able to translate certain features that are specific to Swift. For a list, see Using Swift from Objective-C.

  • If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章