I need to create a static library with Swift, and I need to know how can I implement interface for the library.
我需要用Swift创建一个静态库,我需要知道如何实现库的接口。
In Objective-C I can mark needed headers as public in build phases, but there is not any headers and any interfaces in Swift.
在Objective-C中,我可以在构建阶段将需要的头标为public,但是在Swift中没有任何头标和接口。
What should I do with Swift?
我该怎么处置斯威夫特?
2 个解决方案
#1
19
Simply put: you don't.
简单的说:你不喜欢。
Swift is not a language that separates headers and implementations. When you create a library or framework based on Swift and only for consumption by Swift, the Xcode default build setting of DEFINES_MODULE
already does the job for you. This will create a .swiftmodule
file, which will be used by import
in other Swift projects.
Swift不是一种区分头和实现的语言。当您基于Swift创建一个库或框架并仅供Swift使用时,DEFINES_MODULE的Xcode默认构建设置已经为您完成了这项工作。这将创建一个.swiftmodule文件,它将被导入到其他Swift项目中。
If you want your code to be importable from Objective-C though, you might want to check if the SWIFT_INSTALL_OBJC_HEADER
build setting is also enabled (which it is by default for frameworks as far as I know). Then the Swift compiler will generate a <ProductName>-Swift.h
file for you, which you can import in Objective-C code to access your Swift classes and functions.
如果您希望您的代码可以从Objective-C中导入,那么您可能需要检查SWIFT_INSTALL_OBJC_HEADER构建设置是否也被启用(据我所知,这是框架的默认设置)。然后,Swift编译器将生成一个
#2
5
If you want your Swift
framework to expose certain classes to the interface, simply mark the 'entities' (functions, variables etc.) public
:
如果您希望您的Swift框架向接口公开某些类,只需将“实体”(函数、变量等)标记为public:
public class Class {}
public var variable: Int
public func function() { }
By default, all entities have internal
access.
默认情况下,所有实体都具有内部访问权限。
-
public
entities are intended for use as API, and can be accessed by any file that imports the module, e.g. as a framework used in several of your projects. - 公共实体的目的是用作API,并且可以通过导入模块的任何文件访问,例如在几个项目中使用的框架。
-
internal
entities are available to the entire module that includes the definition (e.g. an app or framework target). - 包括定义的整个模块(例如,应用程序或框架目标)都可以使用内部实体。
-
private
entities are available only from within the source file where they are defined. - 私有实体只能在定义它们的源文件中使用。
Source: the official Swift blog.
资料来源:Swift官方博客。
#1
19
Simply put: you don't.
简单的说:你不喜欢。
Swift is not a language that separates headers and implementations. When you create a library or framework based on Swift and only for consumption by Swift, the Xcode default build setting of DEFINES_MODULE
already does the job for you. This will create a .swiftmodule
file, which will be used by import
in other Swift projects.
Swift不是一种区分头和实现的语言。当您基于Swift创建一个库或框架并仅供Swift使用时,DEFINES_MODULE的Xcode默认构建设置已经为您完成了这项工作。这将创建一个.swiftmodule文件,它将被导入到其他Swift项目中。
If you want your code to be importable from Objective-C though, you might want to check if the SWIFT_INSTALL_OBJC_HEADER
build setting is also enabled (which it is by default for frameworks as far as I know). Then the Swift compiler will generate a <ProductName>-Swift.h
file for you, which you can import in Objective-C code to access your Swift classes and functions.
如果您希望您的代码可以从Objective-C中导入,那么您可能需要检查SWIFT_INSTALL_OBJC_HEADER构建设置是否也被启用(据我所知,这是框架的默认设置)。然后,Swift编译器将生成一个
#2
5
If you want your Swift
framework to expose certain classes to the interface, simply mark the 'entities' (functions, variables etc.) public
:
如果您希望您的Swift框架向接口公开某些类,只需将“实体”(函数、变量等)标记为public:
public class Class {}
public var variable: Int
public func function() { }
By default, all entities have internal
access.
默认情况下,所有实体都具有内部访问权限。
-
public
entities are intended for use as API, and can be accessed by any file that imports the module, e.g. as a framework used in several of your projects. - 公共实体的目的是用作API,并且可以通过导入模块的任何文件访问,例如在几个项目中使用的框架。
-
internal
entities are available to the entire module that includes the definition (e.g. an app or framework target). - 包括定义的整个模块(例如,应用程序或框架目标)都可以使用内部实体。
-
private
entities are available only from within the source file where they are defined. - 私有实体只能在定义它们的源文件中使用。
Source: the official Swift blog.
资料来源:Swift官方博客。