Swift项目中调用Objective-C的库

Swift项目中调用Objective-C的库,第1张

概述这是来自stack overflow上的一个回答,更多回答请查看: http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift Using Objective-C Classes in Swift * If you have an existing class that you’d like

这是来自stack overflow上的一个回答,更多回答请查看:
http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift

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 – .m

Add 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)macroso@H_404_77@thatifyoumoveyourproject,orworkonitwithothersusingaremoterepo,itwillstillwork. @H_516_301@(SRCROOT) macro so that if you move your project,or work on it with others using a remote repo,it will still work. (SRCROOT) can be thought of as the directory that contains your .xcodeproj file. It might look like this:

$(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

import import “CustomObject.h”

@implementation CustomObject

(voID) someMethod {
NSLog(@”SomeMethod Ran”);
}

@end
Step 5: Add Class to BrIDging-header

In YourProject-BrIDging-header.h:

import “CustomObject.h”

Step 6: Use your Object

In SomeSwiftfile.swift:

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.

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}

}
Step 2: import Swift files to ObjC Class

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];
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

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
@objc class PureswiftObject {

var name: Stringinit(name: String) {    self.name = name}// Needed to add a class level initializerclass func newInstancenamed(name: String) -> PureswiftObject {    return PureswiftObject(name: name)}// Just a method for demonstrationfunc someMethod() {    println("Some method ran in pure swift object")}

}
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 con@R_419_6205@icts my approach,please tell me and I’ll update the answer to conform to the suggested style.

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”];
NSLog(@”Pureswiftnamed: %@”,pureswiftObject.name);
[pureswiftObject someMethod];
Note:

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.

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.

@H_670_419@ 总结

以上是内存溢出为你收集整理的Swift项目调用Objective-C的库全部内容,希望文章能够帮你解决Swift项目中调用Objective-C的库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1087119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存