Swift-1-Basic Concepts

Source: Internet
Author: User

//Playground-noun:a Place where people can play
Quick understanding of Swift's common knowledge through code requires a certain object-c base

Import UIKit//declaring constantsLet Maximumnumberofattemps =Ten//declaring Variablesvar currentloginattempt =0//declare multiple constants/variables at the same timevar x =0.0, y =1.0, z =2.0Let a=0.0, B =1.0, C =2.0//Note: If a value should not be changed, it should be declared as let. If a value can be changed, it must be declared as Var//Type callout Types annotationsvar welcommessage:string//declares a variable of type string one time Welcomemessageprintln ("\ (x) is in line: \ (__line__) \nfile: \ (__file__)")//integer vs. floating-point ConversionsLet three =3Let pointonefouronefive=0.1415Let pi= Double (three) +Pointonefouronefivelet PB= three +Int (pointonefouronefive)//TypealiasTypealias CustomInt16 =Int16var ci:customint16= -//BooleanLet Rightisright =trueifrightisright {println ("Right thing")}let I=1ifI//error:type ' Int ' does not conform to protocol ' Booleantype '//cannot use non-bool values as a basis for judging}//tuples meta-groupLet Http404error = (404,"Not Found")//a tuple of type (INT, String)//decomposing tuples [reading values in tuples]//Mode 1Let (StatusCode, statusmessage) =Http404errorprintln ("the status code is \ (StatusCode), and the StatusMessage is \ (StatusMessage)")//Ignore reading some content, ignore the part with _ instead ofLet (StatusCode2, _) =Http404errorprintln ("The status Code 2 is \ (StatusCode2)")//Mode 2: Read directly through the indexLet StatusCode3 = Http404error.0println ("The status Code 3 is \ (STATUSCODE3)")//Method 3: Name the element in the tuple, get it directly from the element nameLet Http404error2 = (code:404, Message:"Not Found") Let StatusCode4=Http404error2.codeprintln ("The status code is \ (STATUSCODE4)")//Note: Tuples are useful when you are temporarily organizing multiple values together, but are not suitable for creating complex data structures. If your data structure is not used temporarily, you should use a class or struct//Optional value Optional: Use optional to indicate a case where the value may be empty.//Nil: Can only be assigned to an optional type. If a constant or variable needs to represent a missing value in a particular case, it must be declared as an optional valuevar serverresponsecode:int? =404 //If there is no initial value of 404, the default initial value of the variable is nilServerresponsecode = Nil//Nil in OC is a pointer to a nonexistent object, and nil in Swift indicates that a variable value of a determined type is missing, and any optional value can be set to nil. //Optional unpacking: Use! To force the optional value to be unpacked, if the optional value is nil, an error occurs, so make sure that the optional value is not nil before forcing unpackingifServerresponsecode! =Nil {println ("Serverresponsecode contains some non-nil value")}//optional binding [optional binding]ifLet code = Serverresponsecode {//if Serverresponsecode! = nil, let code = serverresponsecode!println"Serverresponsecode contains some non-nil value of \ (code)")} Else{println ("Serverresponsecode is nil")}//implicit parsing optional values: The difference between an optional value and a normal is that it can be accessed directly without forcing parsing. However, variables that are implicitly parsed must have a value (non-nil) on each access, or a run-time error will occurLet possiblestring:string? ="a possible string"Let forcedstring= possiblestring!//must be added! For forced parsingLet assumingstring:string! ="An implicitly unwrapped optional string."Let implicitstring= Assumingstring//no need! For forced parsing//Note: If a variable may be nil, it should always be set to optional, rather than an implicitly resolved optional value. //assert assert (consistent with Nsassert in OC)Let age =-3assert ( age>=0,"A person's age connot is less than zero")//will automatically print out the file line

Swift-1-Basic Concepts

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.