Importing Swift into Objective-C
OverviewYou can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target,and you can think of it as an umbrella header for your Swift code. You don’t need to do anything special to create the generated header—just import it to use its contents in your Objective-C code. The header‘s name is generated from your product module name,followed by? The process for importing Swift declarations into Objective-C code differs slightly depending on whether you’re writing an app or a framework. Both processes are described below. Import Code Within an App TargetWhen you‘re building an app target,you can import your Swift code into any Objective-C?
?
By default,the generated header contains interfaces for Swift declarations marked with the? The Swift interfaces in the generated header include references to all of the Objective-C types used in them,so make sure to import the Objective-C headers for those types first. When building an app target,you can provide a custom name for the product module by changing the Product Module Name build setting. Xcode uses this name when naming the generated header file. Import Code Within a Framework TargetTo import a set of Swift files in the same framework target as your Objective-C code,import the Xcode-generated header for your Swift code into any Objective-C? Because the generated header is part of the framework’s public interface,only declarations marked with the? Import Swift code into Objective-C within the same framework:
?
? Include Swift Classes in Objective-C Headers Using Forward DeclarationsWhen declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target,importing the generated header creates a cyclical reference. To avoid this,use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface.
?
Forward declarations of Swift classes and protocols can be used only as types for method and property declarations. https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |