我如何在Objc项目中使用SnapKit?(How could I use SnapKit in Objc project?)

编程入门 行业动态 更新时间:2024-10-25 20:23:30
我如何在Objc项目中使用SnapKit?(How could I use SnapKit in Objc project?)

我用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.

更多推荐

本文发布于:2023-07-17 07:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   如何在   project   Objc   SnapKit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!