文档章节

How to create a pure Swift module

 谁知相恋最长情
发布于 2016/03/05 22:02
字数 510
阅读 17
收藏 0

From scratch, Swift is an innovative programming language for iOS, OS X, watchOS, and tvOS apps that form on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming templates and adds modern features to make programming easier. Moreover, programming with Swift becomes more flexible and fun. Swift’s clean slate, backed by the mature and regularly preferred Cocoa and Cocoa Touch frameworks, is a great chance to reimagine how software development works.

If you are already in touch with Swift, you probably assumed that including third party libraries into your project would be useful. Railsware.com Mobile Developers and Alex Denisov, iOS Engineer and a huge Open Source fan, are ready to talk about it.

Making a custom framework that includes both ObjC and Swift code is very easy because Apple provides a mechanism to distribute code via frameworks (eventually, for iOS too). Next step into proper development is a creation of a pure Swift module, like Apple does with Swift’ std lib and Cocoa/Cocoa Touch bridge.

Basically, we are generating a simple module call Logger that will contain only one method: log.
 You need at least three files to form a Swift module, so we should get all of them as an output:
 Logger.swiftmodule – public interface/definitions
 Logger.swiftdoc – docs (surprisingly)
 libLogger.a – built library (there also might be a dylib, it depends on your task)
 We can start with creating an unserviceable Logger library:

The class just takes some prefix and logs it before actual object:

Now it’s time to make a libLogger.a. The code will look like this:

-emit-library generates dynamically linked shared library, while -emit-object generates object file and includes the main function, so you will have linker errors due to duplicated symbols.
 The solution is simple: include both flags -emit-object and -emit-library, as is depicted above.

The next step is generating Logger.swiftdoc and Logger.swiftmodule:

xcrun swift -emit-module Logger.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name Logger

Now that you have a complete module, it is about time to integrate it into a real project. Create a simple Swift project and add the files:
 

Then setup “Import paths” for Swift:
 

You can now check out if your project works properly:
 

Voila!
 

Dealing with documentation is rather simple, as well. To add documentation to module you need to comment it using ///, e.g.:
 

You will see documentation on the right after its integration:
 

Note: to make it work after integrating, you have to restart Xcode.
 This approach might not be applied for a regular iOS/OSX developer; it requires creating and supporting Make/CMake file, which complicates the process of generation. However, it may be useful if you want to create a pure module that does not include ObjC at all.


本文转载自:

粉丝 0
博文 18
码字总数 9286
作品 0
程序员
私信 提问
Swift_Swift与objC之间的相互调用

假如你现在有一个 swift 类 LoginViewContrller.swift, 但是工程是objc的工程:AppDelegate.h/.m 现在设置AppDelegate的rootviewcontroller为 LoginViewContrller 1. 在target -> build set......

metRooooo
2014/07/08
0
0
rhcad/ShapeAnimation-Swift

ShapeAnimation-Swift Vector animation framework in Swift based on SwiftGraphics for iOS 8. With ShapeAnimation you can easily create various animations with a nice Swift based s......

rhcad
2015/01/22
0
0
kolla-ansible解析

项目地址 https://github.com/openstack/kolla-ansible https://git.openstack.org/cgit/openstack/kolla-ansible/ kolla-ansible部署的大致流程 执行命令Kolla-ansible –i multinode deplo......

JennerLuo
2017/11/01
0
0
新年新语言,WCDB Swift

摘要:WCDB ObjC 版本的实现中,由于引入了 C++ 代码,并不能直接 bridge 到 Swift。因此,我们从 9 月份开始就着手使用原生的 Swift,重写 WCDB。并于 10.10 和 11.8 分别在开发者群内发布了...

WeMobileDev
2018/01/03
0
0
luaJIT FFI Library

LuaJIT FFI Library The FFI library allows calling external C functions and using C data structures from pure Lua code. The FFI library largely obviates the need to write tedious......

epiclight
2015/06/10
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Jenkins系列_插件安装及报错处理

进入Jenkins之后我们可以进行插件的安装,插件管理位于以下模块: 发现上面报了一堆错误,是因为插件的依赖没有安装好,那么这一节,就先把这些错误解决掉吧。解决完成后,也就基本会使用插件...

shzwork
今天
2
0
mysql mysql的所有查询语句和聚合函数(整理一下,忘记了可以随时看看)

查询所有字段 select * from 表名; 查询自定字段 select 字段名 from 表名; 查询指定数据 select * from 表名 where 条件; 带关键字IN的查询 select * from 表名 where 条件 [not] in(元素...

edison_kwok
昨天
8
0
多线程同时加载缓存实现

import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorServi......

暗中观察
昨天
2
0
利用VisualVM 内存查看

准备工作,建几个测试类。等下就是要查看这几个类里面的属性 package visualvm;public class MultiObject { private String str; private int i; MultiObject(String str...

冷基
昨天
2
0
组装一台工作游戏两用机

一、配置清单如下: 分类 项目 价格(元) 主板 华硕(ASUS)TUF Z370-PLUS GAMING II 电竞特工 Z370二代 支持9代CPU 1049 CPU 英特尔(Intel) i7 8700K 酷睿六核 盒装CPU处理器 2640 风扇 九...

mbzhong
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部