@H_502_1@这是来自stack overflow上的一个回答,更多回答请查看:
http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift @H_502_1@Using Objective-C Classes in Swift @H_502_1@* 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
(SRCROOT)macrosothatifyoumoveyourproject,orworkonitwithothersusingaremoterepo,itwillstillwork.
(SRCROOT) can be thought of as the directory that contains your .xcodeproj file. It might look like this:
@H_502_1@$(SRCROOT)/Folder/Folder/<#YourProjectName#>-Bridging-Header.h
@H_502_1@Step 3: Add Objective-C Header – .h
@H_502_1@Add another .h file and name it CustomObject.h
@H_502_1@Step 4: Build your Objective-C Class
@H_502_1@In CustomObject.h
Step 5: Add Class to Bridging-Header @H_502_1@In YourProject-Bridging-Header.h:
instanceOfCustomObject.someProperty = “Hello World”
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()
No need to import explicitly,that’s what the bridging header is for. @H_502_1@Using Swift Classes in Objective-C @H_502_1@Step 1: Create New Swift Class @H_502_1@Add a .swift file to your project,and name it MySwiftObject.swift @H_502_1@In MySwiftObject.swift: @H_502_1@import Foundation @H_502_1@class MySwiftObject : NSObject {
Step 2: Import Swift Files to ObjC Class @H_502_1@In SomeRandomClass.m:
NSLog(@”MyOb.someProperty: %@”,myOb.someProperty);
myOb.someProperty = @”Hello World”;
NSLog(@”MyOb.someProperty: %@”,myOb.someProperty);
Nsstring * retString = [myOb someFunction:@”Arg”];
NSLog(@”RetString: %@”,retString);
Using PURE Swift Classes in Objective-C @H_502_1@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. @H_502_1@Step 1: Create New Swift Class @H_502_1@Add a .swift file to your project,and name it PureSwiftObject.swift @H_502_1@In PureSwiftObject.swift: @H_502_1@import Foundation @H_502_1@// Note ‘@objc’ prefix
@objc class PureSwiftObject {
For this,I create a class initializer called ‘newInstanceNamed:’. Because this class is no longer a descendent of NSObject,it no longer has access to ‘alloc’ or ‘new’. Perhaps there is another workaround,but this is the only way that I have found. I didn’t find any explicit mention of this in the docs. If you do,and it conTradicts my approach,please tell me and I’ll update the answer to conform to the suggested style. @H_502_1@Step 2: Import Swift Files to ObjC Class @H_502_1@In SomeRandomClass.m:
NSLog(@”PureSwiftNamed: %@”,pureSwiftObject.name);
[pureSwiftObject someMethod];
Note:
http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift @H_502_1@Using Objective-C Classes in Swift @H_502_1@* 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 – .m
@H_502_1@Add a .m file to your class,and name it CustomObject.m @H_502_1@Step 2: Add Bridging Header @H_502_1@When adding your .m file,you’ll likely be hit with a prompt that looks like this: @H_502_1@Click YES ! @H_502_1@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 @H_502_1@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: @H_502_1@Note @H_502_1@It’s best practice to link your project using theimport
import “CustomObject.h”
@H_502_1@@implementation CustomObject- (void) someMethod {
NSLog(@”SomeMethod Ran”);
}
Step 5: Add Class to Bridging-Header @H_502_1@In YourProject-Bridging-Header.h:
import “CustomObject.h”
@H_502_1@Step 6: Use your Object @H_502_1@In SomeSwiftFile.swift: @H_502_1@var instanceOfCustomObject: CustomObject = CustomObject()instanceOfCustomObject.someProperty = “Hello World”
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()
No need to import explicitly,that’s what the bridging header is for. @H_502_1@Using Swift Classes in Objective-C @H_502_1@Step 1: Create New Swift Class @H_502_1@Add a .swift file to your project,and name it MySwiftObject.swift @H_502_1@In MySwiftObject.swift: @H_502_1@import Foundation @H_502_1@class MySwiftObject : NSObject {
var someProperty: AnyObject = "Some Initializer Val" init() {} func someFunction(someArg:AnyObject) -> String { var returnVal = "You sent me \(someArg)" return returnVal }@H_502_1@}
Step 2: Import Swift Files to ObjC Class @H_502_1@In SomeRandomClass.m:
import “<#YourProjectName#>-Swift.h”
@H_502_1@The file:<#YourProjectName#>-Swift.h should already be created automatically in your project,even if you can not see it. @H_502_1@Step 3: Use your class @H_502_1@MySwiftObject * myOb = [MySwiftObject new];NSLog(@”MyOb.someProperty: %@”,myOb.someProperty);
myOb.someProperty = @”Hello World”;
NSLog(@”MyOb.someProperty: %@”,myOb.someProperty);
Nsstring * retString = [myOb someFunction:@”Arg”];
NSLog(@”RetString: %@”,retString);
Using PURE Swift Classes in Objective-C @H_502_1@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. @H_502_1@Step 1: Create New Swift Class @H_502_1@Add a .swift file to your project,and name it PureSwiftObject.swift @H_502_1@In PureSwiftObject.swift: @H_502_1@import Foundation @H_502_1@// Note ‘@objc’ prefix
@objc class PureSwiftObject {
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") }@H_502_1@}
For this,I create a class initializer called ‘newInstanceNamed:’. Because this class is no longer a descendent of NSObject,it no longer has access to ‘alloc’ or ‘new’. Perhaps there is another workaround,but this is the only way that I have found. I didn’t find any explicit mention of this in the docs. If you do,and it conTradicts my approach,please tell me and I’ll update the answer to conform to the suggested style. @H_502_1@Step 2: Import Swift Files to ObjC Class @H_502_1@In SomeRandomClass.m:
import “<#YourProjectName#>-Swift.h”
@H_502_1@(if you haven’t already done so) @H_502_1@Step 3: Use your pure swift class @H_502_1@PureSwiftObject * pureSwiftObject = [PureSwiftObject newInstanceNamed:@”Janet”];NSLog(@”PureSwiftNamed: %@”,pureSwiftObject.name);
[pureSwiftObject someMethod];
Note:
- @H_502_1@CodeCompletion wasn’t behaving as accurately as I’d like it to. On my system,running a quick build w/ “cmd + r” seemed to help Swift find some of the Objc code and vice versa.
- @H_502_1@If you add .swift file to an older project and get error: dyld: Library not loaded: @rpath/libswift_stdlib_core.dylib,try completely restarting Xcode.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。