iOS Development: Swift Calls Objective-C Code

iOS Development: Swift Calls Objective-C Code

[[120564]]

Recently, the new iOS programming language Swift has received a lot of attention in the iOS development circle, and there are many tutorials and small examples both at home and abroad. Although the Swift language is still evolving and the changes are still relatively large, and Apple does not promise that the currently written code will be compatible in the future, it still cannot stop the enthusiasm of iOS developers.

Why use Swift to call Objective-C code?

Currently, apps written in Swift can only be submitted using Xcode 6 GM, while Objective-C has been Apple's main development language for many years. Currently, there is no mature Swift library available, so currently writing apps can basically be done by calling Objective-C code.

How to call Objective-C code

The solution currently provided by Apple is to use a Bridging-Header header file to reference the header file of the Objective-C code to be used. The header file name format automatically generated by Xcode will be in the form of project name-Bridging-Header.h. However, this file name format is not required and you can define it freely.

Xcode automatically generates solutions

When you create a new Swift project, it will not automatically generate a header file for calling Objective-C. However, you can create a new Objective-C file in the project. At this time, Xcode will prompt you whether to generate a corresponding header file for Swift to call. Select yes , and a header file in the format mentioned above will be automatically generated. You can delete the newly created empty Objective-C file and only keep this header file.
Just fill in the header file of the Objective-C file or library you want to call later in this bridging header file.

Custom solutions

If you choose to cancel in the previous step, there will be no prompt for creating new Objective-C files in the future. Or you want to customize a file name instead of using the one generated by Xcode.

Select Create a Header file, and the file name can be anything you want. For example, it can be Hello.h
After that, in Project-TARGETS , find Swift Compiler - Code Generation . There is an item here, whose key is Objective-C Bridging Header . Fill in your header file information in its value.

  1. 1. You can drag the corresponding header file to the input box to fill in the value
  2. 2. Manually fill in the header file path into the input box, such as: Hello/Hello.h

In this way, you can use the corresponding header file to reference the Objective-C code.

Original source: http://my.oschina.net/dmdgeeker/blog/318356

<<:  How many employees can follow WeChat Enterprise Account after verification and how to follow them

>>:  See what programmers are reading in September

Recommend

0-budget private domain operation strategy to generate huge fan base!

With 0 budget, increasing the number of followers...

If you drool while sleeping, beware of these 4 diseases! Don't ignore them!

Many people will find their pillows wet with sali...

How does the automotive industry master information flow advertising?

Next week is May Day, which is traditionally the ...

App channel promotion skills and strategies!

As we all know, the first step in App promotion i...

Why do I feel uncomfortable if I don’t drink coffee for a day?

Author: Hao Yun Reviewer: Zhong Kai, Director of ...

For community operation, you need to build a good “personality”!

In community operations , building a good persona...

Frozen steamed buns contain aflatoxin? Can we still eat them?

Recently, a short video claiming that "Zheji...

2016 Network Wish List: Less Hype + More Automation

[[161361]] Wishes don't always come true, but...

Traffic acquisition operator, building matrix traffic

From WeChat business to physical stores, from bra...

Who is the suitable group for Wenchang Tower?

Wenchang Tower applicable scope: (WeChat: 1867393...