Using Swift with Cocoa and objective-c (Swift version 2.0): Start-Basic setup-Standby

Source: Internet
Author: User

This is a profile of an API or technology that is being developed, and Apple provides this information primarily to help you plan through the use of these technologies or programming interfaces with Apple products, which may change in the future, and the software mentioned in this article should be based on the final published operating system tests and final documentation, New versions of document information may be available in the future.

Swift is designed to be seamlessly 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 makes Swift a simple, convenient and powerful tool for integrating into your COCOA application development workflow.

This guide includes three important aspects of compatibility that you can use to better utilize to develop COCOA applications:

    • Interoperability allows you to connect swift and OBJECTIVE-C, allows Swift's class to be used in objective-c, and uses familiar Cocoa class, Pattern, practice when you write swift code.

    • Blending and matching allows you to create mixed-language applications that combine Swift and objective-c files, and they can communicate with each other.

    • Migration as a result of the above two points, migrating from the existing Objective-c code to Swift is very simple, making it possible to use the latest Swift features instead of your OBJECTIVE-C application section.

Before you begin to learn these features, you need to have a general understanding of how to establish 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 a swift-based application.

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 select Swift.

The structure of the SWIFT project is almost identical to the OBJECTIVE-C project, with only one important difference: Swift does not have a header file. There is no division between the implementation and the interface, so all information in a particular class is stored in a separate. Swift file.

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

Understanding the Swift Import process

After you build the Xcode project, you can import any Cocoa platform framework that works with Objective-c in Swift.

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


Import Foundation

This import imports all of the Foundation's APIs, including Nsdate,nsurl,nsmutabledata, and all of their methods, properties, and categories 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 the Objective-c module, which is then imported into Swift as the swift API. 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:

    • Remap determines the type of objective-c to be equivalent in Swift, like ID to Anyobject

    • Remap determines the objective-c core type to the alternative type in Swift, just like NSString to string

    • Remapping determines the concepts of objective-c concepts to be matched in Swift, such as pointers to Optionals

In the Interoperability section, you will learn more about how these mappings are trade-offs in your Swift code. Importing Swift's model to objective-c and importing Swift from Objective-c is very similar. Swift affirms its API, such as a framework for swift modules. Also, these Swift modules 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 have a choice of language features found to be unavailable in objective-c. For more features on using Swift in Objective-c, see Using Swift and Objective-c in the same project.

Note: You cannot import C + + code directly into Swift. The workaround is to create a objective-c or C package for C + + code.

Using Swift with Cocoa and objective-c (Swift version 2.0): Start-Basic setup-Standby

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: 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.