Attention to Swift and objective-c.

Source: Internet
Author: User
Tags uikit

文/仁伯安(授权)原文链接:http://www.jianshu.com/p/2ed48b954612
Objective

Swift has been in operation for several years, with Swift's language mechanism and ease of use more grounded than objective-c, greatly reducing the entry threshold for iOS. Of course, this is not the new children's shoes, is indeed the gospel, but for the entire iOS programming practitioners, it is really, once "tall", suddenly "short dwarfish poor." Plus training courses rampant, bulk wholesale, iOS can no longer see the glory of the year.

The past is no longer mentioned, the matter is still to be done. IOS10 launch, followed by Xcode8 also pushed the update, careful people will find that Xcode8 iOS version of the minimum adaptation has become iOS8.0, coupled with the swift version to stabilize, in a sense, the swift era officially opened, instead of objective-c fear is only a matter of time. Of course, before we do that, we should also be prepared. The more companies that have come this year, the swift and Objective-c have started.

Let's take a look at some of the considerations and issues in the mix today:

Mixed

There are only two cases of mixing,

在Objective - C工程或者文件使用Swift的文件;在Swift工程或者文件使用Objective - C文件

The most important two files in the process of mixing:

1. Bridging files:

The bridging file "Projectname-bridging-header.h" is automatically generated when other files are first created. If you accidentally delete, you can also manually add, but the name must be "projectname-bridging-header.h" header file (name composition: Project name-bridging-header.h), if the name is not clear, you can create a new header yourself After file, the Targets→build settings→swift compiler-general→objective-c Bridging Header profile path, which is primarily used by Swift when using the OC class.

2.objective-c Generated Interface Header name file

This file is mixed when the system generates a Swift file corresponding to the OBJECTIVE-C header file, which can be specified in Targets→build settings→swift compiler-general→objective-c Generated Interface Header name is configured, the default file name is the project name-swift.h, generally do not make changes.

Use Swift files in Objective-c Engineering or documents

When calling the classes in the swift file in the OC file, first add #import in the OC file "

Projectname-swift.h "(Name composition: Project name-swift)

Although this file is not visible in the project, but she really exists, after compiling, you can press and hold command+ click the file name, you will see the specific generated code.

After the introduction, the use of specific classes, directly in accordance with the OC way to use.

Use objective-c files in Swift Engineering or documents

When using OC files in Swift, simply introduce the required header file in the bridging file, the Projectname-bridging-header.h file.

Specific use, according to the corresponding SWIFT syntax structure can be.

Integrated considerations

For swift classes that need to be mixed add @objc declarations or subclasses that inherit NSObject or NSObject

class TestClass{// 属性// 实现}

If you want to use the TestClass class in the Objective-c class, you should declare it using @objc, or inherit testclass from NSObject or nsobject subclasses, otherwise After introducing Productname-swift.h, the program cannot find the corresponding class.

Using the third-party framework
设置: target-->build setting -->Packaging -->Defines Module为 “Yes”;然后,配置文件Target -> Build Phases -> Link Binary,添加要导入的Framework;最后,还是要配置桥接文件,比如要使用 abc-lib.framework库中的 abc.h 就要这样配置:#import"abc-lib/abc.h";
Subclass sub-Class problems

For a custom class, the class of objective-c cannot inherit from Swift's class, that is, the OC class to be mixed cannot be a subclass of the Swift class. In turn, the Swift class that needs to be mixed can inherit from the OC class. Annotations

OC Macro File

If you want to use macros defined in OC for swift files, you can use only constant simple macro files.

Swift's unique features

There are many features of the OC that are not available in swift, such as Swift has tuples, functions for class-one citizens, and unique enumeration types. Therefore, the mixed file to be used should be aware of Swift's unique property issues.

Block with OC in the case of Swift

Using closure in Swift cannot use block as an attribute to pass a value, it must be an initialization method or function.
In the Objective-c file:

#import <UIKit/UIKit.h>typedef void (^Myblock)(NSString *arg); @interface FirViewController : UIViewController //@property (copy, nonatomic) Myblock myBlock; //这种作为公共参数的形式,如果在Swift类中去回调的话,是有问题的。提示没有初始化方法,所以使用下面的以Block为参数的方法 - (void)transValue:(Myblock) block;@end

The following is the. m file

#import "FirViewController.h" @implementation FirViewController - (void)viewDidLoad {     [super viewDidLoad];     self.view.backgroundColor = [UIColor whiteColor]; } - (void)transValue:(Myblock)block{     if (block)     {         block(@"firBack");     } } @end

Callback in Swift file:

When Swift uses the OC class, first declare the OC header file in the bridging file
Project name-bridging-header.h This is the case when you create a swift project

import UIKit class ViewController: UIViewController {    override func viewDidLoad()     {         super.viewDidLoad()         self.view.backgroundColor = UIColor.whiteColor()     }     @IBOutlet weak var goFirst: UIButton!     @IBAction func goFirstAction(sender: AnyObject)     {         let firVC:FirViewController = FirViewController()         firVC. transValue { ( arg:String !) -> Void in             self.aBtn?.setTitle(arg, forState: UIControlState.Normal)        }         self.navigationController?.pushViewController(firVC, animated: true)     }

Attention to Swift and objective-c.

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.