我用CocoaPods创建了一个新项目(Xcode 9.3.1和Swift 4.1),当然在其中使用SnapKit。
我想用第三方类TreeView,这个类由Objc写的。
我如何在类TreeView中使用SnapKit?
我尝试使用以下步骤但不工作:
创建一个名为SnapKit-Bridging-Header.h的新文件
去TreeView.m,导入头文件(注意:它以'-Swift'结尾):
#import“SnapKit-Swift.h”
现在编译OK! 我可以在TreeView.m文件中看到LayoutConstraint(在SnapKit中定义)和其他一些类
但是当我使用view.snp.xxx时,它抱怨“Property'snp'not found ...”
所以我的Q是如何在ObjC文件中使用view.snp.xxx? 谢谢 :)
I create new project with CocoaPods (Xcode 9.3.1 and Swift 4.1)and of course use SnapKit in it.
and I want use third party class TreeView ,this class wrote by Objc.
How cloud I use SnapKit in class TreeView ???
I try to use the following steps but not working:
create new file named SnapKit-Bridging-Header.h
go to TreeView.m , import the header file (notice:It ends with '-Swift'):
#import "SnapKit-Swift.h"
compiling now is OK! and I can see LayoutConstraint (defined in SnapKit)and some other class in TreeView.m file
but when I use view.snp.xxx ,it complain "Property 'snp' not found ..."
So my Q is How could I use view.snp.xxx in ObjC file??? Thanks :)
最满意答案
Swift代码与Objective-C不能完全互操作。 您只能在您的桥接头中使用Objective-C支持的Swift元素。 这个库需要Swift,它并不是说它支持Objective-C,所以很可能你需要做额外的适应工作来支持它。
在iOS 9+中,您拥有的NSLayoutAnchor API几乎与SnapKit一样易于使用(请参阅https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html ; https:// cocoacasts .com / working-with-auto-layout-and-layout-anchors / ; https://theswiftlibrary.com/the-simple-way-of-creating-constraints-in-code )
该API易于使用并与Objective-C兼容。
如果你想支持iOS 8,你可以使用像这样的backporting库。
Swift code is not fully interoperable with Objective-C. You can only use such Swift elements in your bridging header that are supported by Objective-C. This library requires Swift, it doesn't say that it supports Objective-C, so it's likely you need to do extra adaptation efforts to support it.
In iOS 9+ you have NSLayoutAnchor API that is almost as easy to use as SnapKit (see https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html ; https://cocoacasts.com/working-with-auto-layout-and-layout-anchors/ ; https://theswiftlibrary.com/the-simple-way-of-creating-constraints-in-code)
This API is easy to use and compatible with Objective-C.
If you want to support iOS 8, you can use a backporting library like this one.
更多推荐
发布评论