How does Objective c code migrate from Swift code Objective c code to Swift

  • 2020-05-07 20:31:36
  • OfStack

The migration provides an opportunity to review existing Objective-C applications, as well as to better optimize the application's architecture, logic, and performance through Swift code. Directly, you will use the previously learned mix and match and the interoperability between the two languages for incremental migration. The Mix-and-match feature makes it easy to choose which features and functions to implement with Swift and which are still implemented with Objective-C. The interoperability of Swift and Objective-C makes it easy to integrate these functions into Objective-C. With these tools, you can open up Swift's extensions and integrate them into the existing Objective-C project without having to rewrite the entire project with Swift at once.

prepares your Objective-C code for migration

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 the modern features of Objective-C to optimize your existing projects. In order to interact seamlessly with Swift more easily, your existing code needs to follow modern coding practices. For a short list of fitness exercises, see Adopting Mordern Objective-C.

migration process

The most efficient way to migrate code is on a file-by-file basis, one class at a time. Since you cannot inherit the Swift class in Objective-C, it is best to choose one that has no subclasses. Instead of.m and.h, you can use a single.swift file. All of your implementation code and interfaces will be put directly into a single Swift file. You don't have to create header files anymore; Xcode automatically generates header files when you need a reference. This is of course done internally by xcode, which is transparent to developers.

preparation

The & # 8226; : in the Xcode File > New > File > (iOS or OS X) > Other > Swift creates one Swift class for the corresponding Objective-C.m and.h files.
The & # 8226; Import the relevant system framework.
The & # 8226; If you want to access the Objective-C code in the Swift file, you can fill in 1 Objective-C bridge joint. For details, please see Importing Code from Within the Same App Target.
The & # 8226; To make your Swift class accessible in Objective-C, you can inherit the Objective-C class, or tag it with the @objc attribute. Specify a special name for the class to use in Objective-C, tag @objc (#name#), < #name# > This is the Swift class name referenced in Objective-C. For more information, see Swift Type Compatibility.

starts work

The & # 8226; You can integrate the Objective-C behavior by inheriting the Objective-C class, adapting the Objective-C protocol, or more. For more information, see Writing Swift Classes with Objective C Behavior
The & # 8226; 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
The & # 8226; When writing code in Swift that USES the Cocoa framework, remember that certain types are bridged, meaning you can use some Swift types instead of Objective-C types. For more information, see Working with Cocoa Data Types
The & # 8226; When you use the Cocoa design pattern in Swift, see Adopting Cocoa Design Patterns for more general design pattern conversion information.
The & # 8226; For those planning to switch from Objective-C to Swfit, see Propeties.
The & # 8226; If necessary, for Swift properties or methods, use @objc ( < #name# > ) property to provide the Objective-C name, like this:


var enabled: Bool {
    @objc(isEnabled) get {
        /* ... */
    }
}

The & # 8226; instance(-) and class(+) methods are represented by func and class func, respectively.
The & # 8226; Declare simple macros as constants and convert complex macros into functions.

is done

The & # 8226; In your Objective-C code, update the import statement to #import "module name - Swift.h", which was mentioned in Importing Code from Within the Same App Target.
The & # 8226; Remove the check box from the Target member selection box to remove the original Objective-C.m file. Do not immediately delete the.m and.h files for resolution.
The & # 8226; If you give the Swift class a different name, use the Swift class name instead of the Objective-C name.

problem solver prompt

The migration experience varies from project to project. Either way, there are some common steps and tools that can help you solve code migration problems:

The & # 8226; Remember: you cannot inherit Swift classes in Objective-C. Therefore, the classes you migrate cannot have any Objective-C subclasses in your application.
The & # 8226; When you migrate a class to Swift, you must remove the associated.m file from target to avoid compilation errors such as duplicate symbols at compile time.
The & # 8226; In order to be accessible and used in Objective-C, the Swift class must be a subclass of an Objective-C class, or be marked as @objc.
The & # 8226; When using the Swift code in Objective-C, remember that Objective-C does not understand some of the features of those Swift, see Using Swift from Objective-C.
The & # 8226; You can view the generated header file by Commond + clicking on an Swift class name.
The & # 8226; You can view more detailed information, such as its type, properties, and document comments, by clicking on a symbol with Option +.


Related articles: