Using Swift in OC projects

Source: Internet
Author: User

Using Swift in OC projects

This article copyright belongs to the author all, if need reprint please contact Mengxiang month
CSDN Blog: Http://blog.csdn.net/mengxiangyue
Independent Blog: http://mengxiangyue.com

Recently, the company's project began planning to use Swift, because the original project was written using OC, it is not possible to convert all to swift, so the use of OC and swift mixed Way is the best choice. This article is just a simple introduction, and not too advanced knowledge.

I created a demo of OC Project, of course you can use the OC project that you already exist. If we want to use Swift's code in OC Engineering, Swift's code is managed using module by default, and also here we need to expose our swift code as a module to our OC project, modifying the following configuration:

The above modifies a configuration item that has a product Module name that will be used later.

In the project, click File/new/file ..., select Ios/source/cocoa Touch Class, and create a new file as follows:

Subclass of must be set to NSObject or its subclasses, otherwise OC works will not find the class.

Click on the confirmation will choose to save the path, click Create, the following interface appears:

This interface is asking whether to create a bridged header file, which is useful when Swift calls the OC code, but when I invoke Swift in OC I find that there are no eggs.

Select the Don ' t create button.

Add the following code to the Test.swift (the explanation is in the comment):

import UIKit/*    如果Swift类想要被OC发现,必须继承自NSObject并且使用public标记,并且该类中想要被OC访问的方法也必须使用public标记,具体知识可以去看Swift的访问控制    原因:Swift的代码对于OC来说是作为一个module存在的    当然全局的Swift函数,我还没发现怎么在OC中访问,如果哪位清楚还请告诉一下,谢谢!*/publicclass Test: NSObject {    publiclog() {        print("这是Swift的方法")    }}publicglobalLog() {    print("这是Swift全局的log方法")}

We introduce a header file in the method we want to invoke the Swift class: "Product Module Name-swift.h", where the product module Name is replaced with the content shown in the configuration item above, for example:

#import "ViewController.h"// 引入Swift头文件#import "OCAndSwift-Swift.h"@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 使用Swift的类    Test *test = [[Test alloc] init];    [test log];}@end

This allows us to use Swift's code in OC and, finally, to say: "Product Module name-swift.h" (In the example is Ocandswift-swift.h), is generated automatically by the compiler, if you do not prompt after import, compile a bit. This file is automatically generated only if there is at least one swift file in the project, so if the project does not have a swift file, the import file will be error-free even if it is set to the configuration.

Using Swift in OC projects

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.