"We all love Paul Hegarty." Stanford IOS8 public class personal note 9 objective-c compatibility

Source: Internet
Author: User

We all know that iOS development has been using the Objective-c language before Swift was born. A bridging is required between OC and Swift.


The APIs in iOS are basically written by OC many years ago, and now can be used in swift, and even you can't see the difference, it's very natural. There are some special types that you need to be aware of when handing in two languages, which makes it much easier to invoke the iOS API with OC than Swift.

The first is nsstring, this is the previous form of string, in iOS it and swift string can be converted to each other, even when they are not different, if there is a parameter need to pass a nsstring value, you can directly pass a string into, And vice versa, although they are completely different, but you can still think of them as one thing, in Swift, string is not even a class, just a struct, thanks to Apple's staff.

The old version of the array Nsarray, the bridge to Swift is an array of type anyobject, so as long as you see an array of anyobject types in the iOS API, they were nsarray.

Nsdictionary is bridged in swift bonding is a subclass NSObject object, and the value is anyobject.

then int, float, double, bool are all received from the NSNumber Bridge, NSNumber is OC in all about the value of the object, int, float, double these and C language int, float, double is also bridged good, So if the API has a C-language int parameter, it can also accept an int from Swift


Of course, if you are sure what data type you need, you can do type conversion. For example, length is a property of NSString, does not exist in string, it does not exist in string for a reason, and the new string handles the problem of variable Unicode length, while the old NSString does not handle this problem very well.

The bridging rules of the dictionary are confusing because the roots of the key values sting, int, and so on are not objects, and more unlikely to be nsobject, but you can still use them because the numeric types are bridged to the nsobject subclasses. Most of the time you don't see this kind of bridging, because they're all automatic.

"We all love Paul Hegarty." Stanford IOS8 public class personal note 9 objective-c compatibility

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.