文档章节

How to create a pure Swift module

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

精选30+云产品,助力企业轻松上云!>>>

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
157
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
1.1K
0
Generate reports with Seam 3 Reports and Apache...

Generate reports with Seam 3 Reports and Apache Velocity Seam 3 provides a collection of standard CDI extensions. Seam3 report module bridges CDI and several report engines, suc......

hantsy
2012/10/23
99
0
[Swift]在Swift项目中创建桥接头文件,Swift文件和Objective-C文件相互调用

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblogs.com/stren...

osc_pw27dksr
2019/04/10
3
0
新年新语言,WCDB Swift

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

WeMobileDev
2018/01/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

io.DataInputStream

一、前言 本文解决io.DataInputStream.skipBytes(int n) 用于跳过输入流中x个字节的数据 1 语法public final int skipBytes(int n)2 参数n:要跳过的字节数。3 返回值此方法返回要跳过...

osc_i1dzen1k
20分钟前
9
0
深度解读OpenYurt:从边缘自治看YurtHub的扩展能力

作者 | 新胜 阿里云技术专家 导读:OpenYurt 开源两周以来,以非侵入式的架构设计融合云原生和边缘计算两大领域,引起了不少行业内同学的关注。阿里云推出开源项目 OpenYurt,一方面是把阿里...

osc_j9ayg4mu
21分钟前
11
0
跨域三大洲的圆桌讨论:人工智能的未来挑战与突破

     作者 | 蒋宝尚、青暮   编辑 | 丛 末   7月9日,世界人工智能大会开启了一场跨洲连线,参与连线的有六位嘉宾,他们就主题《人工智能的未来挑战与突破》进行了圆桌讨论。    ...

osc_emgrwx5d
23分钟前
13
0
Spring Cloud微服务Sentinel+Apollo限流、熔断实战

在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架。而以下要介绍的正是作者最近两个月的真实项目...

osc_facwbzof
25分钟前
18
0
order by 注入姿势

order by 注入原理 其实orde by 注入也是sql注入的一种,原理都一样就是mysql语法的区别,order by是用来排序的语法。 sql-lab讲解 判断方法 1.通过做运算来判断如:2-1的返回是否和1一样,和通...

osc_tu9yshvx
26分钟前
24
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部