The basic grammar of Swift learning--(i)

Source: Internet
Author: User

Swift has been out for a long time, has not started the study, now began to decide the system of learning.

The beginning of learning in any language is grammar:

 1. Note: Let is modified to be immutable var is variable let maximumnumberofloginattempts = 10;        var currentloginattempts = 0;        currentloginattempts = currentloginattempts + 1;        NSLog ("maximumnumberofloginattempts =%ld", maximumnumberofloginattempts);                NSLog ("currentloginattempts =%ld", currentloginattempts); 2, type annotation: Generally speaking, very few labels are judged by the initial value variable or constant type var welcommessage:string welcommessage = "Hello" Print ("Welcommes Sage = \ (welcommessage))//3, constant and variable naming-you can use any character as a constant and variable name (including Unicode characters), but constants and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode Code bits, lines, and tabs cannot start with numbers, but can contain numbers//constants and variables that cannot be exchanged--but can change the value of a variable to a value of the same type && constant value once determined to be immutable, the change causes a variation error Welcommessage = " New Hello "//3+++, pay attention to the output method of Swift---Print (" New welcommessage = \ (welcommessage) ")//Let LanguageName =                "Swift"//LanguageName = "OC"//Compile Error!!!! 4, tuples--you can combine any sequence of types into a tuple that can contain all types of let Http404error = (404, ' not Found ') print("Http404error = \ (http404error)")//4.1 can decompose the contents of a tuple into separate constants and variables let (statuscode,statusmessage) = Http404error Print ("StatusCode = \ (statusCode), StatusMessage = \ (StatusMessage)")//4.2 if only a subset of the tuple values are needed, you can use the part you want to ignore when decomposing Dash (_) Mark Let (justcode,_) = Http404error print ("Justcode = \ (Justcode)") let (_,justmessage) = Http404er Ror print ("justmessage = \ (justmessage)")//4.3 can access a single element in a tuple by subscript, and the subscript starts with a zero print ("the Statusco De is \ (http404error.0), message = \ (Http404error.1) ")//4.4 can name a single element when defining a tuple, and then get the value of those elements by name let H Ttp200status = (statuscode:200,description: "OK") print ("Http200status = \ (http200status) \ncode = \ (http200status. StatusCode), description = \ (http200status.description))//5, Optional optional--the so-called optional implies that you can "have no value" let Posssiblen        umber:string = "123" Let Possiblenotnumber = "123?" let convertednumber = Int (posssiblenumber) Let convertnotnumber = Int (posSiblenotnumber) Print ("Posssiblenumber = \ (posssiblenumber), possiblenotnumber = \ (possiblenotnumber)") Print ("Convertednumber = \ (convertednumber), convertnotnumber= \ (convertnotnumber)")//6, implicit parsing optional le T possiblestring:string? = "An optional string" print ("possiblestring = \ (possiblestring)") print ("Possiblestring!!!                = \ (possiblestring!) ") Let assumedstring:string! = "An implictitly unwraped Optional string" Print ("assumedstring = \ (assumedstring)") Print ("assumedstring!! !                        = \ (assumedstring!) ")        7. Assertion Let Age =-3 assert (age >= 0, "A person's age can ' t is less than zero")//Because age < 0 will break /* Note: 1, nil in OC and Swift's nil mean is not the same--oc in nil is the Russian pointer to an indeterminate object, nil in Swift is not a pointer, it is a definite value to indicate that it is true that any type of optional can be set to n Il, not just object type 2, if a variable may become nil, do not use implicit parsing optional. If you need to determine if it is nil in the lifetime of the variable, use the normal optional type 3, the assertion's use case: 3.1: The satellite script index of the integer is passed into a custom satellite script implementation,However, the subscript index value may be too small or too large; 3.2: You need to pass a value to the function, but the illegal value can cause the function to not perform properly 3.3: An optional value is now nil, but a non-nil value assertion is required for the subsequent code to run, causing the program to terminate. So--can quickly locate the problem, easy to find the problem * *

This is a simple record of some of the more basic and commonly used parts of individuals.

If there is something wrong, or want someone to tell, thank you!

Reference from: http://www.cocoachina.com/ios/20140612/8776.html

The basic grammar of Swift learning--(i)

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.