Swift Basic Tutorial Notes

Source: Internet
Author: User
Tags switch case

http://www.imooc.com/learn/127

"Fun to Swift" Web tutorial notes, you have updated according to the syntax of 2.1.

I.

1. Learn and familiarize yourself with the features of swift language through playground.

2. A tuple declaration can have a name for each meta, which can then be used. Access

var t= (x:1, y:2, Z:"hi")  //  var t: (int,int,string) var (a,_,_) =t    //  _ anonymous print (T.0)    //  or T.x

3. Optional type

Usage:. ToInt return can be nil

Coercion type conversions cannot be selectable.

can choose the solution package: the choice can be converted to positive type, plus!

If let X=name {} can also be used to unpack

Ii.

1. Int.min

2.a===b or a!==b type comparison

3.Nil coalescing Operator a?? b Note space

4. The range operator a...b A. <b

Inside the 5.for loop, index is constant

Iii.

1.String return length:. Characters.count is so complicated.

2. Interpolation "\ ()"

3.. hasprefix. Hassuffix

. capitalizedstring

. uppercasestring. lowercasestring

4.trim

5.split. componentsseparatedbystring

6.join. Join

7.Range & String.index

. rangeofstring Optional type som 23..<25

. startIndex  . endindex 

    . Startindex.advancedby (7) Use range to take substring, insert, remove, replace and other operations  iv.1. Can I save only one type of Array  ? declaring type array<string> or [String] initializing empty table: (1) var array=[int] () (2) var array: [int]=[]  empty: array=[] Assignment:  array =[int] (count:10,repeatedvalue:8) operation:  .count. isEmpty. Append + = [x]. Insert. Removeatindex (0). Removelast (). R Emoverange    array[2...4]=["HI"]. Sort (<) traversal: (1) for in   (2) 0. <array.count   (3) enumerate (array)       2.Dictionary  declaring type dictionary < String,int> or [string:int] initializing empty table: (1) var dictionary =[int:string] () (2) Var array: [int:string]=[]  Empty: dictionary =[:] Operation:  .count. isEmpty. Updatevalue  .removevalueforkey Dictionary[x] Returns an optional type (none returns nil)     Delete value is set to nil traversal: (1) for (Key,value) in   (2). Keys (3). Values       &N Bsp          //Force type Conversion Array (Dictionary.keys)    v. You don't need to add (), the code snippet must be {}1. Loop structure (1) For in   (2) for  ;  ;   ; (3) while (4) does while2. Selection structure (1) if Else (2) switch does not need to write break, the same branch with commas in the same case  switch case:      (1) CA SE (_,1), ( -2 ... 2,-2...2), value binding case (Let x,1) (2) Case-let (x, y) where X==y: (3) null-explicit Break (4) Fallthrough      Default 3.break, continue can specify which layer to jump out of the loop
 for inch m{    for with  t{         if o==1{              break  mainloop     ;     }}}

Vi.func

1.func sayhi (name:string?), string{}

(1) Return is empty, can not write return, can also write return Void or ()

(2) returns multiple return values, which can return a single tuple

(3) Incoming, return can be optional (flexible application?) 、! 、?? , nil)

2. Parameters (1) external parameter name + internal argument name, #name also indicates that the parameter name (2) has a default value parameters must have a foreign parameter name (the default is the same as the reference name); parameters without default values have to be placed in front, sequentially passed in (3) variable parameter type Int ... When using args as an array (4) parameter, the default is a constant parameter. Variable parameter, which declares the name of the parameter plus var. (5) Pass the reference, declare the parameter with the argument name before the InOut, and call the arguments when the parameter is added &. (Note the difference from the C language) 3.function as variable, type is (,), (), such as (Int,int) INT//Better implementation logic(1) The function as a parameter of another function, such as the sorted function. (2) function as the return value of another function.  (3) function nesting. Encapsulation Vii. closures//Python-like Lambda 1. Basic
Inch    

2. Simplifying

(1) Type inference

inch return A>b}

(2) A sentence can be omitted to return

(3) Omit the name of the parameter. {$0> $ {}

3.trailing closure

4.capture values

5. Functions and closures are reference types.

Func calctotalmiles (todaymiles:int),Int  {    var totalmiles =0     return {totalmiles + = Todaymiles;    return totalmiles;}} var dailytwomiles = Calctotalmiles (2)

Viii. Enumeration

enum plant{    case

1. Known types, omit the notation. Earth

2. Original value

enum Planet:int {    case1

Month (rawvalue:12) optional type

Swift Basic Tutorial Notes

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.