Record some changes in the code format of Swift3.0

Source: Internet
Author: User

A. Weight:

1> Color:

Uicolor. Whitecolor () was changed to Uicolor. White ()

2> Array Values:

List. Objectatindex (i) was changed to list. Object(at:i)

3>present:

Presentviewcontroller (Controller, animated: true) was changed to present (Toviewcontroller:controller, animated: true)

4> Cell:

Dequeuereusablecellwithidentifier ( "Cell", forIndexPath: Indexpath) was changed to Dequeuereusablecell (withidentifier: "Cell", For:indexpath)

5> TableView

func Span class= "Hljs-title" >numberofsectionsintableview (Tableview:uitableview), int changed to func Numberofsections (in tableview:uitableview), int

< Span class= "hljs-literal" > two. Parameters

The override Func Viewwillappear (Animated:bool) is replaced by the override Func Viewwillappear (_ Animated:bool) to see an instance of the invocation example, deepening the impression: Func MyFunc (X:int, y:int) This function must be called with the following code MyFunc (x:1, Y:2) If the anonymous parameter is allowed, modify it: Func MyFunc (_ X:int, _ Y:int) This function is allowed to be called without the parameter name Myfun C (1, 2)

Third, the name

1>对齐方式:

lbl.textAlignment = NSTextAlignment.Center

被改为

lbl.textAlignment = NSTextAlignment.center

2>cgrect:

Tablefooterview = UIView (frame: Cgrectzero) changed to Tablefooterview = UIView (frame: Cgrect.zero)

3>cgrectzero:

Cgrectzero Change to Cgrect.zero

4>hidden:

Lbl.hidden = false is changed to Lbl.ishidden = false

5>bundle:

Let bundle = Nsbundle.mainbundle () is changed to let bundle = Bundle.main ()

< Span class= "hljs-built_in" >< Span class= "hljs-literal" >6>filemanager:

< Span class= "hljs-built_in" >< Span class= "hljs-literal" > Let mgr = nsfilemanager.defaultmanager () was changed to let mgr = filemanager. default ()

Iv. return value processing of methods

1> we may often invoke some methods with return values in development, but do not handle return values, such as the following:

navigationController!.popViewControllerAnimated(true)

This method actually returns a uiviewcontroller, but few people use it, and many more scenarios use it as a no-return method. However, in Swift 3, this is not possible, and you must dispose of the return value as follows:

let _ = navigationController!.popViewController(animated: true)

Use a single underscore to refer to a variable that will not be used.

2>swift 3 no longer allows incoming outgoing objects, and the method declaration with Var previously will all be invalidated:

Func MyFunc (Var a:int) and Int such declarations, which are no longer available, must be changed to a method with multiple values to return to: Fun MyFunc (A:int)--(int, int)

V. Optional type


var str: String! = "a"
var s = strprint(s)

六、Selector

Swift 3 has more stringent handling of optional types and must explicitly use exclamation marks anywhere, for example we have the following code:
Self.performselector (onmainthread: #selector (handle (ret:)), With:ret, Waituntildone:true) in Swift3This function as the benchmark bar, the old version of the selector get method is this: #selector (Viewcontroller.handle (_:))//2.2#selector (Viewcontroller.handle (:))//2.1#selector (handle)//2.0@selector ("Handle:")//1.x x equals a few already don't remember"Handle:"                               //If I remember correctly, it's 1.0 times, just a string is Selector .N/A//In the history of Swift, there are really no Selector versions.

Back to Swift 3, the current Selector is written as the top of the list, it should be noted that the Selector method name and parameter name must be the actual method called exactly the same, or the compilation will be error. < 2.0 is the same as can be called!!! >

In addition, Selector can only pass the object, not the basic data type, the basic data type of the case, all will become 0 (hope this is only the current version of the bug, or too much pain). Although Apple has removed most of the NS class prefixes, but nsnumber this thing still have to use a lot of it?


Seven, class library
Optional 1> protocol
@objc protocol Myprotocol:nsobjectprotocol {      optional func foo (myclass:myclass?)      Optional func bar (Myclass:myclass? )} needs to be changed to @objc protocol Myprotocol:nsobjectprotocol {      @objc optional func foo (myclass:myclass?)      @objc optional func bar (Myclass:myclass? )}

2> Graphic image gallery:

1"\ (Fileutils.getdocumentpath ())/\ (name)"imgdataTrue 1 " \ (Fileutils.getdocumentpath ()!)   /\ (name!) " NSData (data:imgdatatrue)

Reference Link: http://gold.xitu.io/entry/576bd4595bbb500059463426



 

Record some changes in the code format of Swift3.0

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.