Objective-c and Swift

Source: Internet
Author: User

1. First create obj-c/Swift project

2. Create the Swift/obj-c file, which is where Xcode prompts you to create a bridging file between Swift and Obj-c, and click Confirm.


3. In the production xxx-bridging-header.h, import the files that need to be used in swift.

4. Calls can be made using Swift's syntax in swift files

Example:

Objective-c (abc.h)

+ (NSString *) Strbyswift;

Called in Swift

Abc.strbyswift ()

NOTE * * *

Xcode automatically converts the OC method to use in Swift, with the same method name.


Methods for calling Swift in Obj-c

1. target-"Build Setting->package defines module must be set to YES,PRODUCT module name set to XXX, default to project name, do not modify

2. In the obj-c file that needs to be called, import the header file "Xxx-swift.h", XXX for the previous set module NAME.

3. Use OC syntax in OBJ-C code to invoke the method inside Swift

Example:

Swift

Class Swifttest:nsobject {func swiftstr ()->nsstring {return ' Swift string '}}

Called in Objective-c

Swifttest *swifttest = [[Swifttest alloc] init]; [Swifttest SWIFTSTR];


Objective-c and Swift

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.