Swift第一日

为了做一款mac平台上的软件,我打算开始学习Swift。时间不多,每天拿出一到两个小时来学习Swift。与以往不同的是这次我采用Swift官方的英文教程进行入门,用于探索新的学习语言的姿势。

Data Types

Fundamental Types

Int , Double , Float , Bool , String

Primary Collection Types

Array , Set , Dictionary

Advanced Types

Tuple

Declaring Constants And Variables

Declaring Constants

let a = 'AEIOU'
a = 'x' // error 

Declaring Variables

var count = 0
count = 1 

Declaring On Single Line

var a = 1, b = 2, c = 3 

Type Annotations

var count: Int = 0
var hello, world:String 

String Interpolation

By \()

var count = 0;
print("Current Count is \(count)") 

Comments

Single-Line

var a = 1 // single line commments 

Multiline

/*
Multiple lines 
*/ 

Semicolons are only required if you want to write multiple seperate statements on single line.

Numbers

Integer

4 forms and 2 types

signed

let f1 = Int8.max
let f2 = Int16.max
let f3 = Int32.max
let f4 = Int64.max 

unsigned

let f5 = UInt8.max
let f6 = UInt16.max
let f7 = UInt32.max
let f8 = UInt64.max 

On 32-bit platform

print(Int.max == Int32.max) // true
print(UInt.max == UInt32.max) // true 

On 64-bit platform

print(Int.max == Int64.max)
print(UInt.max == UInt64.max) 

Floating-Point Numbers

Different decimal

let bInt = 0b1001
let oInt = 0o12
let hInt = 0xee 

Optional exponent

print(1.23e4 == 12300) // true 
print(0xFp2 == 15 * 2 * 2) // true 
print(0x1.2p0) // 1.125
print(0x1.2) // compile error 

Numeric Type Conversion

let three = 3
let pointOneFourOneFiveNine = 0.14159

let pi = three + pointOneFourOneFiveNine // compile error 
let three = 3
let pointOneFourOneFiveNine = 0.14159

let pi = Double(three) + pointOneFourOneFiveNine 

But

let pi = 3 + pointOneFourOneFiveNine 

This is because 3  don't have any explict type

Type alias

typealias AudioSample = UInt16 

Same as type declaring in TypeScript

Bool

let t = true 

Tuples

let http404Error = (404, "NOT Found")

let (code, message) = http404Error

print(code)

let http404Error = (404, "NOT Found")
// ignore parts of tuple
let (code, _) = http404Error

print(code)
// using index numbers
let message = http404Error.1

print(code)

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

print(http200Status.message)

Optional

let covertedNumber = Int("12312")
print(covertedNumber)
// Optional(12312) 

This means convertedNumber  might contain Int value, or it might contain no value at all.

We can set a optional value

var serverResponseCode: Int? = 404
serverResponseCode = nil 

Or without inital value

var idcard: String?

print(idcard == nil) // true 

We know a optional value will wrapped by Optional()  when output by print , if we knew it has a value, how to get the value?

just using exclamation mark

var serverResponseCode: Int? = 404

if serverResponseCode != nil {
   print("I knew serverResponseCode has a value: \(serverResponseCode!)")
}

We can also use optional binding  to unwrap optional value

var possibleNumber = "123"

if let actualNumber = Int(possibleNumber) {
   print("\"\(possibleNumber)\" could be converted to \(actualNumber)")
} else {
   print("\"\(possibleNumber)\" could not be converted to an integer")
} 

And sometimes it's clear an optinal value will have a value after the value is first set. For this situation, we can  place a exclamation mark before type.

var assumedString: String!

assumedString = "121" 

This is same as TypeScript.

class A {
 private assumedString!: String;
} 

Error Handling

Assertions