A library that calls objective-c in a swift project

Source: Internet
Author: User

Here is an answer from the stack overflow, more answers, please see:
Http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift

Using objective-c Classes in Swift

* If you have a existing class that is you ' d-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 the adding your. m file, you'll likely be hits with a prompt that looks like this:

Click YES!

If you do not see the prompt, or accidentally deleted your bridging headers, add a new. h file to your project and name it < #YourProjectName #>-bridging-header.h

In some situations, particularly if working with OBJC frameworks, you don ' t add an objective-c class explicitly and xcod E can ' t find the linker. In this case, the create your. h file named as mentioned above, then make sure the link its path in your target ' s project sett Ings:

Note

It ' s best practice to link your project using the (S RC ROOT )maCR osoT haT IF youmoveyouR PR oJ eCT ,oR WoR k oNIT WIT hoT heR susINgaR emoT eR ePo,IT WIl l sT Il l WoR k . (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's 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 SE E 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 is accessible and usable in objective-c, a Swift class must is a descendant of an objective-c Class or it must be marked @objc. " Because Our first example are a descendant of NSObject, the compiler does this automatically. Let's look at an example class, that's not a descendant of a 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 have access to ' alloc ' or ' new '. Perhaps there is a another workaround, but the "the only" and "this" has found. I didn ' t find any explicit mention of the "this" in the docs. If you don't, and it contradicts 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 do so)

Step 3:use your Pure Swift class

Pureswiftobject * Pureswiftobject = [pureswiftobject newinstancenamed:@ "Janet"];
NSLog (@ "pureswiftnamed:%@", pureswiftobject.name);
[Pureswiftobject SomeMethod];
Note:

    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.

    2. If you add. Swift file to a older project and get error:dyld:Library not loaded: @rpath/libswift_stdlib_core.dylib, try Completely restarting Xcode.

A library that calls objective-c in a swift project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.