Use of Swift basic data types

Source: Internet
Author: User

////Viewcontroller.swift//Basic data Types////Created by Ye Shi on 16/9/8.//copyright©2016 year Ye Shi. All rights reserved.//Import UIKitclassViewcontroller:uiviewcontroller {Overridefunc viewdidload () {super.viewdidload ()// Let//Constants//var//variables//                //the Let value cannot be changed using let must initialize                /*character, String*/        //charactervar charvaluel:character ="Q"        //stringvar charvalue2:string ="123"                //initialize an empty stringvar charValue3=""var charValue4=String ()//determines whether a string is empty        ifcharvalue2.isempty {print ("string is empty")        }        //or                ifCharValue3 = =""{print ("string is empty")        }                //Assigning a value to a stringLet emptystring ="ABC"Let emptyString2= String ("ABC")                //to traverse a character in a string         forCinchemptystring2.characters {print (c)}/** Yuan zu*/                //the values within the progenitor can be of any type, and the ancestors do not have to be of the same type//define a meta-ancestorLet product= ( -,"iphone 6s",6088) print (product)//get every element in the meta-ancestorvar (_year, _name, _price) =Product Print ("Year = \ (_year)","_name=\ (_name)","price=\ (_price)")                //enter only one valueLet (_, Name1, _)=Product Print ("name\ (name1)")                //You can use dot syntaxLet product2= (Year: -, Name:"iphone 6s", Price:6088) print (product2.year) print (product2.name) print (Product2.price)/*Optional Type*/var stringvalue:string//Print (stringvalue) compilation does not pass, no initializationvar stringvalue2:string?if(StringValue2! =Nil) {Print (StringValue2!)        }        //If the string is "" Use! will print crash use? output to nil//that means there must be a value.var numstr:string ="123"var value1:int? =Int (numstr) print (value1)/** Number worth readability*/Let readabilty1=100000000; Let Readabilty2=1_000_000_000_1/** Type alias*/                //A type alias is one that takes a more meaningful alias (the new type) to a type, using the Typealias keyword//The format is as followsTypealias Newint=Int32 var new_value:newint=123print (New_value)/** Conversions between basic data types*/                //convert int type to other typevar j:int=3Double (j) Float (j)//String type go Int Float Double typevar s:string="2.156ASD2"                //when the first character is not a number, a Double is 0//when the first digit of a character is a number, it can be converted directly to a number, and a number that precedes a non-character is stopped.Int (s) print ("-------", s) var s2= S asnsstring Print (s2.integervalue) print (s2.floatvalue) print (S2.doublevalue) //Double Float Int turn Stringvar d:double=1.68        "\ (d)"var f:float=3.134        "\ (f)"var i:int=1        "\ (i)"                //You cannot use String (INT) directly to set the numeric value of a character in the form of "\ (value)"//int can be used directly with Double (int) Float (int)                /** operator*/                /** ++i is i= i+1 and-i is i = i-1*/        //Unary OperatorsLet three =-3Let minusthree= -three let Plusthree= -minusthree Print (Minusthree, plusthree)//As type conversion//is type check                                                                                                            }    Overridefunc didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.    }}

Use of Swift basic data types

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.