Swift learning the fourth day of Swift's interaction with OC

Source: Internet
Author: User



Interoperability is a feature that enables swift and objective-c to be interfaced, enabling you to access and use code written in another language in a file written in one language. When you are ready to start incorporating swift into your development process, you should know how to use interoperability to





Initialize (initialization) in order to instantiate the Objective-c class with Swift, you should call one of its initializers using swift syntax. When the Init method of objective-c changes to Swift, they are rendered with swift initialization syntax. The "init" prefix is truncated as a keyword to indicate that the method is an initialization method. The Init method, which begins with "Initwith", is also removed. This part of the method, separated from "init" or "initwith", becomes lowercase and is treated as the parameter name of the first argument. Each of the remaining method names becomes the parameter name in turn. These method names are called in parentheses. As an example, you would do this when you use OBJECTIVE-C:
UITableView *mytableview = [[UITableView alloc] 




In Swift, you should do this:

let myTableView: UITableView = UITableView(frame: CGRectZero, style: .Grouped) 




You don't need to call Alloc,swift to handle it for you.  Note that "Init" does not appear when using the Swift-style initialization function. You can explicitly declare the type of an object at initialization time, or you can ignore it, and Swift's type interface correctly determines the type of object.
 
//Swift 
let myTextField = UITextField(frame: CGRect(0.0, 0.0, 200.0, 40.0)) 



For the sake of unification and simplicity, the Objective-c factory method is also mapped in Swift as a convenient initialization method. This mapping allows them to use the same simple and straightforward initialization method. For example, in objective-c you might call a factory method like this:
 
//Objective-C 
UIColor *color = [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0]; 




In Swift, you should do this:
 
//Swift 
let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0) 




Access attributes (accessing properties) use point syntax when accessing and setting properties for Objective-c objects in Swift
 
///Swift 
myTextField.textColor = UIColor.darkGrayColor() 
myTextField.text = "Hello world" 
if myTextField.editing { 
    myTextField.editing = false 
}




When the Getting or setting attribute is used, the property name is not added, and no parentheses are required. Note that a pair of parentheses is appended to the Darkgraycolor, because Darkgraycolor is a class method of Uicolor, not a property. In Objective-c, a parameterless method with a return value can be used as an implicit access function (implicit getter) and can be called using the same method as the accessor. However, in swift it is no longer possible to do so, only attributes written using the @property syntax in Objective-c can be introduced as attributes. See Working with Methods for details. Use the point syntax when using the method (working with Methods) to invoke the Objective-c method in Swift. When the Objective-c method transitions to Swift, the first part of Objective-c's selector will be the base method name and appear before the parentheses, and the first parameter will appear directly in parentheses with no parameter name. The rest of the parameter names and parameters are one by one corresponding to the parentheses. As an example, you would do this when you use OBJECTIVE-C:
 
 
//Objective-C 
[myTableView insertSubview:mySubview atIndex:2]; 




In Swift, you should do this:
 
//Swift 
myTableView.insertSubview(mySubview, atIndex: 2) 



If you call an parameterless method, you must still add a pair of parentheses after the method name
 
 
//Swift 
myTableView.layoutIfNeeded() 

Swift learning the fourth day of Swift's interaction with 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.