Objective-c works with Swift

Source: Internet
Author: User

Original http://blog.csdn.net/xuanwenchao/article/details/30226823

1. We add the Swift class to the existing Objective-c project. Cmd+n is shown below

2. Click Next and enter Sfloginview language to select Swift

3. Click Next to pop up the causeway, whether to create a bridge file, only the first time the creation will appear, create a second class if you have previously created a hint, the file is used in Swift to use the Objective-c class, the introduction of the header file.

4. Click Yes to create two files, as shown, my project is called Sepass, the generated bridge file is called Sepass-bridging-header.h

5. Open the Sfloginview.swift file and create one of our swift classes, of course you can also use the playground file to edit and copy to the swift file.

6. Now suppose that we need to use a objective-c class in Swift, called GTMBase64, at which point we just open the bridged file we just generated sepass-bridging-header.h to reference the header file we want (#import "GTMBase64.h").

7. Once the header file is referenced, the class can be used directly.

var imagedata:nsdata = GTMBase64. Decodestring (G_dataimagekey)

8. Now that the class Sfloginview we have written with Swift has been completed, we are going to refer to our Swift-written class in the existing objective-c, because the Swift class has no header files, and we cannot import them directly. So we're going to help him. Generate a header file that can be referenced, point to project file, and select Targets-buildingsettings, Product Module Name we enter Whatwhatwhat

9. At this point, Xcode generates a header file, whatwhatwhat-swift.h we can use the Sfloginview class only after we reference it in the oc.m file that we want to use Sfloginview.

#import "Whatwhatwhat-swift.h"

10. Let's see what's in the whatwhatwhat-swift.h.

It appears that the system helped us to generate a class implementation of the objective-c that corresponds to the Swift class

11. The following is an official note, once you import the SWIFT code into your project, you can use the standard ocjective-c to operate the Swift class

Objective-c works with Swift

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.