Swift interacts with OBJECT-C (Swift version: 1.2)

Source: Internet
Author: User
Tags instance method

This article mainly introduces the code of interaction between Swift and Object-c, which is divided into two parts. One is the Object-c class that the SWIFT project calls, and the other is the OBJECT-C project that calls the Swift class. The process is simple, but it is still a bit difficult for people who have not been exposed to it.

You can download this article on GitHub related source code: swift-objective-c-mutual (https://github.com/GarveyCalvin/Swift-Objective-C-Mutual)

First, Swift calls Object-c

Now, to get to the point, the goal is to "call Object-c with Swift", so let's have a look at it first. We need to verify the following issues:

1. How to invoke the Object-c class method;

2. How to invoke the Object-c instance method;

3. How to access the properties of Object-c

We created a new Swift project, and after the new project is completed, add the person class and add the language to object-c.

Clicking Next will have a pop-up window asking if you need to build a bridge, or a bridge to connect with the object-c. It will help us to build a header file named "Project name-bridging-header.h", of course we choose yes here.

Our structure directory becomes this, and the new bridged file here is named "Swiftcallobject-c-bridging-header.h".

When this file is built, it does not automatically help us to import the header file, so we need to import the Object-c class header file that needs to be called so that our Swift can call object-c correctly.

// // Use the This   file to import your target 's public headers so would like to expose to Swift. //  "Person.h"

Let's take a look at the code for the person class, this class has a name attribute, and a print instance method, and a personwithname: method, mostly demonstrating how to invoke these. First look at Person.h.

////Person.h//swiftcallobject-c////Created by Baijiawei on 15/5/14.//Copyright (c) 2015 Garveycalvin. All rights reserved.//#import<Foundation/Foundation.h>@interfacePerson:nsobject@property (Strong, nonatomic) NSString*name;- (void) print;+ (ID) Personwithname: (NSString *) name;@end

Person.m.

////person.m//swiftcallobject-c////Created by Baijiawei on 15/5/14.//Copyright (c) 2015 Garveycalvin. All rights reserved.//#import "Person.h"@implementation Person- (void) print{NSLog (@"name is%@", Self.name);}+ (ID) Personwithname: (NSString *) name{ person*person =[[Person alloc] init]; Person.name=name; returnPerson ;}@end

The calling method is as follows.

// instantiating an object " Garvey " // Accessing Object Properties // Calling Object Methods  = Person.personwithname ("Calvin"as//  Call Person's class method, returning an instance of it //  Calling the object method

Second,object-c calls Swift

Similarly, we need to create a object-c project and then create a Swift class. Let's start by creating the person class.

If you are the first to create a Swift class in the OBJECT-C project, there will still be a window prompt asking if you need to create a bridging file. However, there is a problem, if you choose No, meaning is not to create a bridge file, then it will not default to help you generate the corresponding calling code; If you choose Yes, it will help you generate the corresponding calling code by default. So what does that mean exactly?

Now let's take a look at the code for the Person class, which is simple, with only one method and one property.

 // //  //  Object_ccallswift    // //  //  Copyright (c) Garveycalvin 2015. All rights reserved.  // import COCOA@OBJC  class   Person:nsobject {var name:string ?  Func print () {println ( "  person name is \ (name)   " )}}  

Note: Object-c The SWIFT code, the Swift class needs to add the keyword @ "@objc" before class, otherwise Xcode will not maintain the header file for you.

As a first note, when calling Swift in Object-c, Xcode will help us to automatically generate the header file without the need for us to maintain it. There is a premise, however, that you must create a bridge file, otherwise ....

When we need to use it, the file name "project name-swift.h" is introduced.

#import " object_ccallswift-swift.h "

After introduction we can click on the header file, just mentioned the file a few times, and now we look at the code to see the file, only to intercept the key parts.

We can see the relevant code for the person class, style is object-c, it should be Xcode for us to transcode. Keep in mind that when Object-c calls the swift-side code, the Swift class needs to have the "@objc" keyword in front of it.  

Problems

An error occurred while introducing the header file

Online object-c Project calls OBJECT-C code will encounter problems, now online popular articles, most of them are in error, as long as you are not careful, you are likely to be "network."

For example is this article: http://www.ituring.com.cn/tupubarticle/2687. The article error point, #import "project name-swift.h":

Wrong in Helloworld-swift.h, where Swift's opening letter S must be uppercase! I was for this reason, looking for a long time did not find out, but also because too much trust on the internet for the sake of the article (here I am not blaming the author). This may be the result of a change made by Xcode after the update or Swift language update.

There is a way to confirm the full name of your bridge file, which can be seen in the settings of the project:

Can see very clearly, the correct wording is "project name-swift.h", as long as this is filled in, the subsequent calls are much simpler.

Blog Garveycalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

This article copyright belongs to the author and the blog Garden altogether, welcome reprint, but must retain this paragraph statement, and gives the original link, thanks cooperation!

Swift interacts with OBJECT-C (Swift version: 1.2)

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.