swift3.0 and Objective-c Interaction Matters (RPM)

Source: Internet
Author: User


Reprint: swift3.0 and objective-c interactions need to be noted for these



Chapter, Author: zeroj (Gitbub)



Swift3.0 out, you can see the change is great, and cocoa, Foundation ... Interactions have also become more convenient, and Swift is writing apps that fit into iOS 7, so we can be confident that the use of swift in the future will gradually increase, and will involve the coexistence of OC and Swift in the project, where I reread the official ' using Swift with Cocoa and Objective-c (swift3) ' documents, recorded some personal feel more commonly used notes, please read (the code is from the document)



The OC initialization method is cited in Swift as


Init (...)---if initialization does not fail init? (...) ---If initialization may fail init! (...) ---otherwise


The property in OC (getter==, setter==) will be ignored by Swift



ID corresponds to Anyobject but all anyobject are optional in swift, and if the previous value is an optional value, it is a multiple selectable value after it is set to Anyobject.



The attributes in OC are marked as


Nullable in Swift? Nonnull-in Swift equals the non-optional attribute, not labeled, in Swift!


The lightweight generics in OC are also corresponding to the generics in Swift


@property Nsarray *dates corresponds to var dates: [Date]


Closures in swift capture variables by default in the same way that a block in OC captures a variable marked as __block, that is, a pointer to a variable that is captured by a closure



In Swift, as long as it is not in multi-threading, it is recommended to use [unowned self] to avoid circular references, in multi-threading, it is recommended to use [weak self]



The = = operator is equivalent to isequal in OC:---that is, comparing content is equal; = = = equivalent to a pointer comparison in OC



Subclasses inheriting from NSObject if you override the Isequals: method, you should provide the hash property



Cannot inherit Swift's class in OC


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.