简体   繁体   English

在objc中使用Swift框架

[英]Using swift framework in objc

I just created a framework in swift, and its working fine in swift project but in objective c I am getting errors like Undefined symbols for architecture armv7: . 我刚刚在swift中创建了一个框架,并且在swift项目中也可以正常工作,但是在目标c中,我遇到Undefined symbols for architecture armv7:等错误。 I just checked the available architectures using lipo -info , its showing both simulator and device architectures. 我只是使用lipo -info检查了可用的体系结构,它显示了模拟器和设备体系结构。 Also I referred this link but no use . 我也提到了这个链接,但没有用 Attached image also. 也附有图片。 I'll appreciate any help, Thank you. 谢谢您的帮助,谢谢。

Here below is to do list in your Framework. 以下是您框架中的待办事项列表。

Swift classes usage in Objective C classes Swift在Objective C类中的使用

  • Use Open Keyword before class name in Swift 在Swift中,在类名之前使用Open Keyword
  • Use @objc Keyword before Open Keyword of Swift class 在Swift类的Open关键字之前使用@objc关键字
  • Import all header of objective c classes in Umbrella file YourProject.h those are consuming Swift Classes. 将目标c类的所有标头导入正在使用Swift类的Umbrella文件YourProject.h中。
  • Use #import in Objective c 在目标c中使用#import

I followed Apple Mix and Match approach. 我遵循了Apple Mix and Match的方法。 Hope this will help. 希望这会有所帮助。 Thanks 谢谢

To access a swift class in objc, that is not inherited from NSObject you need to: 要访问不是从NSObject继承的objc中的swift类,您需要:

@objc public class yourUtils @objc公共类yourUtils

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. Swift类或协议必须使用@objc属性标记,才能在Objective-C中访问和使用。 This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. 该属性告诉编译器可以从Objective-C访问这段Swift代码。 If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you. 如果您的Swift类是Objective-C类的后代,则编译器会自动为您添加@objc属性。

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM