Swift项目中调用Objective-C的库
这是来自stack overflow上的一个回答,更多回答请查看: Using Objective-C Classes in Swift * If you have an existing class that you’d like to use,perform Step 2 and then skip to Step 5. (For some cases,I had to add an explicit #import Step 1: Add Objective-C Implementation – .mAdd a .m file to your class,and name it CustomObject.m Step 2: Add Bridging Header When adding your .m file,you’ll likely be hit with a prompt that looks like this: Click YES ! If you did not see the prompt,or accidentally deleted your bridging header,add a new .h file to your project and name it <#YourProjectName#>-Bridging-Header.h In some situations,particularly when working with ObjC frameworks,you don’t add an Objective-C class explicitly and Xcode can’t find the linker. In this case,create your .h file named as mentioned above,then make sure you link its path in your target’s project settings like so: Note It’s best practice to link your project using the
$(SRCROOT)/Folder/Folder/<#YourProjectName#>-Bridging-Header.h Step 3: Add Objective-C Header – .h Add another .h file and name it CustomObject.h Step 4: Build your Objective-C Class In CustomObject.h importimport “CustomObject.h”@implementation CustomObject
@end In YourProject-Bridging-Header.h: import “CustomObject.h”Step 6: Use your Object In SomeSwiftFile.swift: var instanceOfCustomObject: CustomObject = CustomObject() Using Swift Classes in Objective-C Step 1: Create New Swift Class Add a .swift file to your project,and name it MySwiftObject.swift In MySwiftObject.swift: import Foundation class MySwiftObject : NSObject { var someProperty: AnyObject = "Some Initializer Val" init() {} func someFunction(someArg:AnyObject) -> String { var returnVal = "You sent me (someArg)" return returnVal } } In SomeRandomClass.m: import “<#YourProjectName#>-Swift.h”The file:<#YourProjectName#>-Swift.h should already be created automatically in your project,even if you can not see it. Step 3: Use your class MySwiftObject * myOb = [MySwiftObject new]; As pointed out by @Tomá?Linhart in the comments,“To be accessible and usable in Objective-C,a Swift class must be a descendant of an Objective-C class or it must be marked @objc.” Because our first example is a descendant of NSObject,the compiler does this automatically. Let’s look at an example class that is not a descendant of an Objective-C Class. Step 1: Create New Swift Class Add a .swift file to your project,and name it PureSwiftObject.swift In PureSwiftObject.swift: import Foundation // Note ‘@objc’ prefix var name: String init(name: String) { self.name = name } // Needed to add a class level initializer class func newInstanceNamed(name: String) -> PureSwiftObject { return PureSwiftObject(name: name) } // Just a method for demonstration func someMethod() { println("Some method ran in pure swift object") } } Step 2: Import Swift Files to ObjC Class In SomeRandomClass.m: import “<#YourProjectName#>-Swift.h”(if you haven’t already done so) Step 3: Use your pure swift class PureSwiftObject * pureSwiftObject = [PureSwiftObject newInstanceNamed:@”Janet”];
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |