Swift 基础知识

前端之家收集整理的这篇文章主要介绍了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:

  • var myVariable = "The width is "
  • myVariable = 50
  • let myConstant = 42
  • 在上面的eg1中,我们的变量myVariable并没有明确的给他一个数据类型,常量myConstant也是没有明确的给它一个数据类型。因为编译器会根据我们给的初始值区推断我们的常量或是变量是什么类型的数据。但是一个常量或是一个变量必须有一个同一类型的值。
  • A constant or variable must have the same type as the value you want to assign to it. However,you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type. In the example above,the compiler infers that myVariable is an integer because its initial value is an integer.

但是在声明变量的时候,我们也可以这样给它一个明确的数据类型

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:

  • welcomeMessage = "Hello"

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!”:(简短理解就是,变量是可以修改的)

  • var friendlyWelcome = "Hello!"
  • friendlyWelcome = "Bonjour!"
  • // friendlyWelcome is now "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:

  • let languageName = "Swift"
  • languageName = "Swift++"
  • // this is a compile-time error - languageName cannot be changed

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.

变量和常量的名称是唯一的,且声明以后你不能修改数据类型。

二、打印输出print

The 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*/

  • /* this is also a comment,
  • but written over multiple lines */

第三种:

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:

  • /* this is the start of the first multiline comment
  • /* this is the second,nested multiline comment */
  • this is the end of the first multiline comment */

这个写法是不是感觉再也不用担心在写/**/注释的时候里面多写了/*或是*/了。

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:

  • A decimal number,with no prefix 十进制
  • A binary number,with a 0b prefix 二进制
  • An octal number,128)"> 0o prefix 八进制
  • A hexadecimal number,128)"> 0x prefix 十六禁止
@H_180_301@ eg5:

All of these integer literals have a decimal value of 17:

  • let decimalInteger = 17
  • let binaryInteger = 0b10001 // 17 in binary notation
  • let octalInteger = 0o21 // 17 in octal notation
  • let hexadecimalInteger = 0x11 // 17 in hexadecimal notation

2.浮点数

在Swift中,提供了两种signed的浮点数:

  • Double represents a 64-bit floating-point number.
  • Float represents a 32-bit floating-point number.

数据的强制转换:

eg6:

  • let three = 3
  • let pointOneFourOneFiveNine = 0.14159
  • let pi = Double(three) + pointOneFourOneFiveNine
  • // pi equals 3.14159,and is inferred to be of type Double
  • let integerPi = Int(pi)
  • // integerPi equals 3,and is inferred to be of type Int

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:

  • let http404Error = (404,"Not Found")
  • // http404Error is of type (Int,String),and equals (404,"Not Found")


  • let (statusCode,statusMessage) = http404Error
  • print("The status code is \(statusCode)")
  • // prints "The status code is 404"
  • print("The status message is \(statusMessage)")
  • // prints "The status message is Not Found"

如果我们只需要元组中得部分值,我们可以把不需要的使用”_”代替。

eg8:

  • let (justTheStatusCode,_) = http404Error
  • print("The status code is \(justTheStatusCode)")
  • // prints "The status code is 404"

如果我们需要其中的某一个元素我们也可以使用下标索引的形式取值:

eg9:

  • print("The status code is \(http404Error.0)")
  • print("The status message is \(http404Error.1)")
  • // prints "The status message is Not Found"

元组也可以定义为单个元素名:

eg10:

let http200Status = (statusCode: 200, description: "OK")

  • print("The status code is \(http200Status.statusCode)")
  • // prints "The status code is 200"
  • print("The status message is \(http200Status.description)")
  • // prints "The status message is OK"

猜你在找的Swift相关文章