Swift Learning (1 basic grammar)

Source: Internet
Author: User
Tags unpack

Swift Basic Syntax

Basic

1. Pre-processing commands are canceled

2. Cancellation of the concept of pointers

3. The NS prefix is canceled

4. Replacing a class with a struct

5. ";" In the same row used to split the statement, if not the same row can be omitted

6. Optional options

7.playground

8. Conditional statement "If", Empty Execution "()"

9. No non-0 is true logic

10.switch (1) can be branched for any type of value, not confined to integers

(2) and generally do not need to break.

(3) can use "," to split optional values

(4) All branches require at least one instruction, and if no instruction is used, break

11. The traditional for loop has been canceled. Unified for in, and supports inversion (reverse order), etc.

12.i++, ++1, I-,-i have been canceled.

13.swift There is no automatic layout of the code, only with frame or xib

Optional values

1. Use! Unpack

2. Use the?? Unpack (actually a special three-mesh), lower priority, general () package

3. Use syntax if Let/var to determine if the value of the object is nil, no unpacking after entering the branch

4. Syntax Guard Let

1.{} Special usage in OC, Common TableView Packaging

Strings string (note difference nsstring)

1. Support Traversal. Characters

2.lengthofbytes returns the length of bytes in the specified encoding state

3.characters.count return the length of the presentation (recommended)

4. Convert a string to NSString

5. Splicing of character changer

6. Using Format Strings

7. Substring of a string

Type conversion AS

Array

1.swift storage type can be stored in the underlying data type and structure does not need packaging, OC needs packaging Nsnumber,nsvalue (packaging structure)

2. If the type in the array type is inconsistent, it is automatically deduced into NSObject

Note that in the CG structure, it is necessary to use Nsvalue packaging

3.swift also has a type,anyobject-> arbitrary object, can not have the parent class, this and OC all classes have the parent class is different

4. Iterating through an array

(1) As with OC directly with [] to subscript for I in 0..<array.count

(2) As with OC, Traverse for s in array with for

(3) Same as OC with simultaneous traversal of subscript and content for E in array.enumerated ()

(4) Swift-specific colleague traversal subscript and content for (n,s) in array.enumerated ()

(5) Reverse-order traversal for s in array.reversed ()

(6) Reverse sequence traversal subscript & content (first enumerated after reverse order) for (n,s) in array. enumerated (). Reversed ()

5. Array additions and deletions

(1) The array in OC can be divided into immutable nsarray and variable nsmutablearray,swift using Let,var to declare

(2) Append element array.append ("111")

(3) Modification by subscript positioning modification array[0] = "222"

(4) Delete array.remove (at:3), Array.removeall (keepingcapcity:ture) (remove but reserve space)

6. Array initialization variable group, the initial capacity of 0, if inserted, if also to insert capacity is not enough, then capacity = current capacity. If you initialize the specified capacity, you can avoid repeated allocation of space when inserting

var array = [int] ()

7. Merge the array var array1, let Array2.

(1) Array1 + = Array2 The types of arrays to be merged must be identical, different types

Dictionary

1. Dictionary definition, used in OC {}, Swift []. For example Let dict = ["Name": "Zhang San", "Age": 10]

Exception: Let array= [[string:anyobject]] = [[' Name ': ' Zhang San ', ' Age ': ' [' Name ': ' John Doe ', ' age ': 15]]

2. Dictionary additions and deletions are variable and immutable in OC, the LET definition in Swift is immutable, var definition is variable

var dict = ["Name": "Zhang San". " Age ": 15]

New If key does not exist is new

dict["title" = "eldest brother"

Modify if key exists or is modified

dict["name"] = "John Doe"

Delete-Specify key Delete

Dict.removevalue (forkey: "Name")

Note: The dictionary is located by key, and the key must be hash (each key can be converted to a unique integer)

3. The traversal of the dictionary, OC Traversal can only be evaluated by traversing key

For e in Dict e is a value pair with E.key e.value value

for (Key,value) in Dict key,value are respectively evaluated

11. Merging of Dictionaries

var dict1 = ["Name": "Zhang San". " Age ": 15]

Let dict2 = ["Name": "John Doe", "title": "Eldest brother"]

Dictionaries cannot be added directly to the array!!!

For E in Dict2 {

Dict1[e.key] = Dict2[e.key]

}

The merging process, if key exists just modified, does not exist is new

Demo:https://github.com/fushengit/learn-swift

Swift Learning (1 basic grammar)

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.