Swift, OBJECTIVE-C, Cocoa mixed programming settings Guide _swift

Source: Internet
Author: User
Tags mixed

Swift is designed to be seamless and compatible with Cocoa and objective-c. In Swift, you can use the Objective-c API (including the system framework and your custom code), and you can also use the Swift API in Objective-c. This compatibility enables Swift to become a simple, convenient, and powerful tool to integrate into your COCOA application development workflow.

This guide includes three important aspects of compatibility to make it easier for you to develop COCOA applications:
Interoperability allows you to connect Swift with OBJECTIVE-C, allowing the use of Swift's class in Objective-c and using familiar Cocoa class, pattern, and Practice when writing Swift code.
Blending and matching allows you to create hybrid language applications that combine Swift and objective-c files, and they can communicate more with each other.
Migration due to the above two points, migrating from existing Objective-c code to Swift is very simple, making it possible to replace your OBJECTIVE-C application part with the latest Swift features.

Before you start learning these features, you need to have a general idea of how to build a Swift environment to access the COCOA system framework.
Build your Swift Environment

To begin the experience of accessing the COCOA framework in swift, use a template from Xcode to create an application based on Swift.
Create a Swift project in Xcode

1 Select File > New > Project > (IOS or OS X) > Application > Your template of choice.

2 Click the Language pop-up menu and choose Swift.

The SWIFT project's structure is almost exactly the same as the Objective-c project, with only one important difference: Swift has no header files. There is no division between implementations and interfaces, so all information in a particular class is stored in a separate. Swift file.

Now, you can start to experience writing Swift code in app delegate, or you can create a swift by selecting File > New > File > (iOS or OS X) > Other > Swift Class.

Understand the Swift import process

After you have established the Xcode project, you can import any COCOA platform framework that objective-c to work in Swift.

Any objective-c framework (or class C library) will be used as a module to import directly into Swift. These include all OBJECTIVE-C system frameworks-such as Foundation, Uikit, and Spritekit, just as the system supports public class C libraries. For example, if you want to import Foundation, simply add the import statement to the top of the Swift file you wrote.

Copy Code code as follows:

SWIFT
Import Foundation

This import imports all Foundation APIs, including Nsdate,nsurl,nsmutabledata, and all of their methods, properties, and classes can be used directly in Swift.

The import process is very concise. The OBJECTIVE-C framework declares the API in the header file. In swift, those header files are compiled into Objective-c module, which is then imported into Swift's API as Swift. The import determines how Objective-c's Function,class,method and Type appear in Swift. For Function and method, this process affects their parameters and return values. The import process can do the following things:

1. Re-mapping the OBJECTIVE-C type identified to the same type in Swift, just like ID to Anyobject
2. objective-c core type identified by remapping to the alternative type in Swift, just like NSString to string
3. Re-mapping the objective-c concepts identified in Swift to match concepts such as pointers to Optionals

In the interoperability section, you'll learn more about how these mappings will be made in your Swift code. Importing Swift's model to objective-c and importing Swift from Objective-c is very similar. Swift declares its APIs, such as a framework for swift modules. At the same time, these Swift modules will generate OBJECTIVE-C header files. These header files can be mapped back to the Objective-c API. Some Swift APIs do not map back to Objective-c because they choose the language features that are not available in objective-c. For more features of using Swift in Objective-c, see Using Swift and Objective-c in the same project.

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.