之前已经导入了整个的XMPPFramework:
现在代码中使用:
1
|
var curXmppStream: XMPPStream?
|
但是出错:
Use of undeclared type ‘XMPPStream’
[解决过程]
1.试着:
已经去导入了那个:
SwiftXMPP-Bridging-Header.h
1
|
#import "XMPP.h"
|
但是错误依旧。
2.然后突然注意到:
之前导入的整个XMPPFramework,但是文件夹后面带问号?
3.搜:
swift Use of undeclared type ‘XMPPStream’
swift undeclared XMPPStream
参考:
此处点击进入,是可以看到源码的:
![can see XMPPStream source code](https://i-blog.csdnimg.cn/blog_migrate/f17ec07cfce10a76247e6e6b13b5b1b9.jpeg)
然后也是可以看到对应的symbol
![reveal in symbol navigator](http://i1.wp.com/www.crifan.com/files/pic/uploads/2016/08/reveal-in-symbol-navigator.jpg?resize=615%2C550)
搜:
undeclared XMPPStream
swift obj class undefined
swift call objective c class undeclared
参考:
然后去另外那个可以正常编译使用XMPPStream的项目,找到了对应的bridge header的设置地方:
![found Object-C Bridging Header settings](http://i0.wp.com/www.crifan.com/files/pic/uploads/2016/08/found-Object-C-Bridging-Header-settings.jpg?resize=615%2C250)
所以去当前项目,添加对应的头文件:
Build Settings-》(点击All可以看到)Swift Compiler - Code Generation-》Object-C Bridging Header-》添加对应的头文件(注:1.如果有子目录路径,则需要添加子目录路径,2.是子目录,不是group组)
![swiftxmpp bridging header added](https://i-blog.csdnimg.cn/blog_migrate/ca78f0194a6e8f471b4844ee78684dc3.jpeg)
其中,此处没有添加,所谓的目录:
Control/RegisterAndLogin
因为此处是group,而不是子文件夹:
![xcode folder is group not folder](https://i-blog.csdnimg.cn/blog_migrate/fd9cd2d8fb0977393c633e9fba510629.jpeg)
即可正常编译,且可以查看简介:
![can see simple intro](http://i2.wp.com/www.crifan.com/files/pic/uploads/2016/08/can-see-simple-intro.jpg?resize=615%2C295)
[总结]
Swift中调用Object-C代码(库)的的话,需要:
1.先去建立一个桥接的头文件,然后里面加上对应的import对应的头文件
比如:
SwiftXMPP-Bridging-Header.h
内容为:
1
2
3
4
5
|
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "XMPP.h"
|
2.然后,在Swift的项目属性中的,添加此头文件作为Object-C的桥接头文件
Build Settings-》(点击All可以看到)Swift Compiler - Code Generation-》Object-C Bridging Header-》添加对应的头文件(注:1.如果有子目录路径,则需要添加子目录路径,2.是子目录,不是group组)
3.然后Swift中即可直接使用对应的类了:
比如,直接使用XMPPStreamDelegate和XMPPStream:
1
2
3
4
5
6
|
import UIKit
class
LoginViewController: UIViewController, UITextFieldDelegate, XMPPStreamDelegate {
。。。
//XMPP server related
var curXmppStream: XMPPStream?
|
即可。