Swift 基础知识
一、常量与变量1.常量与变量的表示在Swift中实用let表示常量,使用var表示变量。Use let to make a constant and var to make a variable。一个常量的值在编译时不需要知道,但你必须一次赋值(The value of a constant doesn’t need to be known at compile time,but you must assign it a value exactly once.) eg1:
但是在声明变量的时候,我们也可以这样给它一个明确的数据类型 eg2: var welcomeMessage:String “Declare a variable called welcomeMessage that is of type String.” The welcomeMessage variable can now be set to any string value without error:
2.什么时候使用常量和变量NOTE If a stored value in your code is not going to change,always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change. 简单翻译就是在代码中需要改变的使用变量,不需要改变的使用常量。 eg3: You can change the value of an existing variable to another value of a compatible type. In this example,the value of friendlyWelcome is changed from "Hello!" to "Bonjour!”:(简短理解就是,变量是可以修改的)
Unlike a variable,the value of a constant cannot be changed once it is set. (常量声明后就不能改变)Attempting to do so is reported as an error when your code is compiled:
3.常量与变量的命名规则Constant and variable names can contain almost any character,including Unicode characters: Constant and variable names cannot contain whitespace characters,mathematical symbols,arrows,private-use (or invalid) Unicode code points,or line- and box-drawing characters. Nor can they begin with a number,although numbers may be included elsewhere within the name. 常量和变量的名称可以保函任何字符,包括Unicode字符。但是不能使用数字符号、空格、箭头和无效的Unicode代码或是线框绘制字符,不能使用数字开头。 Once you’ve declared a constant or variable of a certain type,you can’t redeclare it again with the same name,or change it to store values of a different type. Nor can you change a constant into a variable or a variable into a constant. 变量和常量的名称是唯一的,且声明以后你不能修改数据类型。 二、打印输出printThe print(_:separator:terminator:) function is a global function that prints one or more values to an appropriate output. In Xcode,for example,the print(_:separator:terminator:) function prints its output in Xcode’s “console” pane. The separator and terminator parameter have default values,so you can omit them when you call this function. By default,the function terminates the line it prints by adding a line break. To print a value without a line break after it,pass an empty string as the terminator—for example,print(someValue,terminator: ""). eg4: print("The current value of friendlyWelcome is (friendlyWelcome)") 三、注释注释部分Swift中是写了三种,个人感觉就是对/* Comments */注释做了扩展。 第一种:// // this is a comment 第二种:/* Comments*/
第三种: Unlike multiline comments in C,multiline comments in Swift can be nested inside other multiline comments. You write nested comments by starting a multiline comment block and then starting a second multiline comment within the first block. The second block is then closed,followed by the first block:
这个写法是不是感觉再也不用担心在写/**/注释的时候里面多写了/*或是*/了。 Nested multiline comments enable you to comment out large blocks of code quickly and easily,even if the code already contains multiline comments.(嵌套多行注释使你注释掉大块的代码快速方便,即使代码已经包含多行注释。) 四、基本数据类型1.整数整数Integers分为signed(包括正数,零和负数)和unsigned(正数和零)。 Swift provides signed and unsigned integers in 8,16,32,and 64 bit forms. These integers follow a naming convention similar to C,in that an 8-bit unsigned integer is of type UInt8,and a 32-bit signed integer is of type Int32. Like all types in Swift,these integer types have capitalized names. 不同进制数据的表示方法: Integer literals can be written as:
eg5: All of these integer literals have a decimal value of 17:
2.浮点数在Swift中,提供了两种signed的浮点数:
数据的强制转换: eg6:
3.布尔类型在Swift中,提供了true and false。 4. Tuples(元组)Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other. 元组中的值可以是任何类型的而且不必是同一类型的。感觉比OC里面的数组还要强大。 eg7:
如果我们只需要元组中得部分值,我们可以把不需要的使用”_”代替。 eg8:
如果我们需要其中的某一个元素我们也可以使用下标索引的形式取值: eg9:
元组也可以定义为单个元素名: eg10: let http200Status = (statusCode: 200, description: "OK")
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |