swift 4数据类型

2018-05-30  本文已影响17人  艺术农
let wantADouble = 3 //let wantADouble: Int = 3
let actuallyDouble = Double(3)
let actuallyDouble: Double = 3
let actuallyDouble = 3 as Double
//定义字符类型需要显示指定其类型
let characterA: Character = "a"
let characterDog: Character = "🐶"
let stringDog: String = "Dog"
let stringDog = "Dog" // Inferred to be of type String
var message = "Hello" + " my name is "
let name = "Matt"
message += name // "Hello my name is Matt”

//非字符串类型拼接需要强制类型转换
let exclamationMark: Character = "!"
message += String(exclamationMark) // "Hello my name is Matt!”

let zhang = "zhang"
let number = 3
var zhangsan = zhang + String(number)
message = "hello my name is \(name)" // "Hello my name is Matt!"
let oneThird = 1.0 / 3.0
let oneThirdLongString = "One third is \(oneThird) as a decimal."

“One third is 0.3333333333333333 as a decimal.”

以这种方式插入double类型的缺点是无法控制输出长度

let bigStr = """
    you can have a string
  that contains multiple
    lines
  by doing
  this.
  """
print(bigStr)
  1. 不带变量名元组的定义与访问
let coordinate3D = (1, 2.1, 3)
print(coordinate3D.0, coordinate3D.1, coordinate3D.2)//通过索引来访问
  1. 带变量名元组的定义与访问
let coordinate3D = (x: 1, y : 2.1, z : 3)
print(coordinate3D.x, coordinate3D.y, coordinate3D.z)
let (x3, y3 ,z3) = coordinate3D
print(x3, y3, z3)
  1. 下划线
    如果你想忽略元组某个部分,可以用下划线来替代这个位置。
let (x4, y4, _) = coordinates3D
上一篇 下一篇

猜你喜欢

热点阅读