How objective-c code is ported to the SWIFT code objective-c code transfer to Swift process introduction _swift

Source: Internet
Author: User

Migration provides an opportunity to re-examine existing objective-c applications, as well as the Swift code to better optimize the architecture, logic, and performance of your application. Directly, you will use the previously learned mix and match and the interoperability between the two languages for incremental migration work. The Mix-and-match feature makes it easy to choose which features and functions to use with Swift and which are still implemented with OBJECTIVE-C. The interoperability of Swift and objective-c makes it not difficult to integrate these features into Objective-c. These tools allow Swift's extended functionality to be opened and integrated into existing OBJECTIVE-C projects without the need to immediately rewrite the entire project with Swift.

Prepare for Migration for your objective-c code

Before you start migrating your code, make sure you have the best compatibility between your objective-c and Swift code. This means organizing and using Objective-c's modern features to optimize your existing projects. To make it easier to seamlessly interact with Swift, your existing code needs to follow modern coding practices. Here's a short list of fitness exercises, see adopting Mordern objective-c.

Migration process

The most efficient way to migrate your code is based on a file-by-document basis, which completes one class at a time. Since you cannot inherit the Swift class in Objective-c, it is best to choose a child-free (translator: From the perspective of the inheritance of classes, you should start with the leaf node of the class family tree and migrate from the bottom up). You can use a single. Swift file instead of the corresponding. m and. h files. All of your implementation code and interfaces will be placed directly into a single Swift file. You don't have to create a header file anymore; Xcode will automatically generate headers when you need to refer to them. (Translator: Of course, this really xcode the internal mechanism is complete, transparent to the developer)

Preparatory work

• Create a Swift class in Xcode:file>new>file> (IOS or OS X) > Other > Swift for the corresponding OBJECTIVE-C.M and. h files.
• Import the relevant system framework.
• If you wish to access the OBJECTIVE-C code in the Swift file, you can fill in a objective-c bridge connector. For specific procedures, see importing Code from Within the Same App Target.
• To make your Swift class accessible in objective-c, you can inherit the Objective-c class, or tag the @objc attribute. Specify a special name for the class to use in Objective-c, Mark @objc (#name #), < #name #> is the Swift class name referenced in Objective-c. For more information, see Swift Type compatibility.

Start work

• You can integrate the Swift class into objective-c behavior by inheriting objective-c classes, adapting objective-c protocols, or more. For more information, please see writing Swift Classes with Objective-c Behavior
• When you use objective-c APIs, you need to know how Swift translates certain objective-c features. For more information, see Interacting with objective-c APIs
• When you use Swift to write code that uses the COCOA framework, remember that certain types are bridged, meaning that you can use some of the swift types to replace the objective-c type. For more information, see Working with Cocoa Data Types
• When you are using the Cocoa design pattern in Swift, see adopting Cocoa design patterns Get more information about the conversion of the general pattern.
• For those who intend to convert items from objective-c to Swfit, see propeties.
• Provide objective-c name, as necessary, for Swift's properties or methods through the @objc (< #name #>) attribute, like this:

Copy Code code as follows:

var Enabled:bool {
@objc (isenabled) get {
/* ... */
}
}

• The instance (-) and Class (+) methods are represented by Func and class Func, respectively.
• Declare simple macros as constants to convert complex macros into functions.

Done

• Update the import statement in your objective-c code to #import "module name-swift.h" mentioned in importing code from Within the Same App target.
• Remove the check box from the target's member selection box, removing the original objective-c.m file. Do not delete the. m and. h files immediately for problem resolution.
• If you give the Swift Class A different name, use the Swift class name instead of the objective-c name.

Problem Solving Tips

For different projects, the migration experience is not the same. In any case, there are common steps and tools that can help you solve the problem of code migration:

• Remember: You cannot inherit the Swift class in Objective-c. Therefore, the class you are migrating cannot have any objective-c subclasses that exist in your application.
• When you migrate a class to Swift, you must remove the associated. m file from target to avoid compile errors such as duplicate symbols that are prompted at compile time.
• In order to be accessible and used in Objective-c, the Swift class must be a subclass of a objective-c class, or be marked as @objc.
• When you use the SWIFT code in Objective-c, remember that OBJECTIVE-C does not understand some of the features of Swift, see Using Swift from Objective-c.
• Commond + click on a Swift class name to view the header file he generated.
• Use option + click a symbol to view more detailed information, such as its type, attributes, and documentation comments.

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.