Migrating objective-c code to SWIFT (Swift 2.0 update)-B

Source: Internet
Author: User

This section includes the following:

    • Prepare your Objective-c code for Migration (Preparing Your objective-c Codes for Migration)

    • Migration processes (the migration process)

    • Problem solving tips (troubleshooting tips and Reminders)

Migration provides an opportunity to revisit existing objective-c applications and to better optimize the architecture, logic, and performance of the application by replacing some of the swift code. In other words, the so-called incremental app migration is to allow you to use the previous tools-mix and match to improve interoperability with each other. When it comes to choosing which features and features to use with Swift, and which ones are still implemented with OBJECTIVE-C, Mix-and-match makes it all easier. The interoperability of Swift and objective-c makes it not difficult to integrate these functions into objective-c. These tools allow you to open Swift's extended functionality and integrate into existing OBJECTIVE-C projects without having to immediately rewrite the entire project with Swift.

Prepare your objective-c code for Migration

Before you begin migrating your code, make sure that you have the best compatibility between your objective-c and Swift code. This means organizing and using the modern features of objective-c to optimize your existing projects. For easier and more seamless interaction with Swift, your existing code needs to follow modern coding practices. Before you begin, there is a short list of adaptation exercises, see adopting Mordern objective-c.

Migration process

The most efficient way to migrate code is to complete a class one at a time, based on a file-by-document basis. Since you cannot inherit the Swift class in Objective-c, it is best to choose one without subclasses (translator: From the inheritance point of the class, 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 automatically generates a header file when you need to reference it. (Translator: Of course, this is done within Xcode, which is transparent to the developer)

Preparatory work

    • In Xcode:file>new>file> (IOS or OS X) > Other > Swift creates a swift class for the corresponding OBJECTIVE-C.M and. h files.

    • Import the relevant system framework.

    • If you want to access the OBJECTIVE-C code in a Swift file, you can fill in a objective-c bridge connector. For specific steps, see importing Code from within the same App Target.

    • To enable your Swift class to be accessed in objective-c, you can inherit the Objective-c class, or tag the @objc attribute on it. Specify a special name for the class to use in Objective-c, mark on @objc (#name #), < #name #> is the Swift class name that is referenced in objective-c. For more information, see Swift Type compatibility.

Start working

    • You can let the Swift class integrate objective-c behavior by inheriting the Objective-c class, adapting the OBJECTIVE-C protocol, or a few more ways. For more information, see Writing Swift Classes with objective-c Behavior

    • When you use Objective-c APIs, you need to know how Swift is translating certain OBJ Ective-c characteristics of the. For more information, see Interacting with objective-c APIs

    • When writing code with the COCOA framework in Swift, remember that some types are bridged, meaning that you can use some swift types instead of Ob The jective-c type. For more information, see Working with Cocoa Data Types

    • When you use the Cocoa design pattern in Swift, see  adopting Cocoa designs Patterns get more The conversion information of the multi-purpose design pattern.

    • For the person who intends to convert the project from Objective-c to Swfit, see  properties.

    • If necessary, provide a objective-c name for Swift's properties or methods through the @objc (< #name #>) property, just like this:

12345 varenabled: Bool {    @objc(isEnabled) get {        /* ... */    }}

The instance (-) and Class (+) methods are represented by the Func and class Func respectively.

Declare a simple macro as a constant and convert a complex macro to a function.

Done

    • Updating the import statement in your objective-c code is #import "module name-swift.h", which is mentioned in importing code from within the same App target.

    • Remove the original objective-c.m file by removing the checkbox in the Target's member selection box. Do not delete the. m and. h files immediately for problem resolution.

    • If you have a different name for the Swift class, use the Swift class name instead of the objective-c name.

Problem Resolution Tips

Although the migration experience is not the same for different projects, there are some 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 classes that you migrate cannot have any objective-c subclasses in your application.

    • When you migrate a class to Swift, you must remove the associated. m file from Target to avoid compilation errors such as duplicate symbols appearing 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 Swift code in OBJECTIVE-C, remember that OBJECTIVE-C does not understand certain features of Swift, see Using Swift from Objective-c

    • You can view the header file that it generates by Commond + clicking a Swift class name.

    • You can view more detailed information, such as its type, attributes, and document comments, by using OPTION + click a symbol.

Migrate objective-c code to SWIFT (Swift 2.0 update)-B

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.