Swift入门(一)

前端之家收集整理的这篇文章主要介绍了Swift入门(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 常量、变量
    声明常量:let
    声明变量:var
    eg. let conNum = 1; var num = 2;

  2. 基本数据类型
    Int、 Float、Double、Bool、tuples、optional
    其中
    tuples 元祖,可以包含不同类型值的集合;
    optional 可选值,表示一个值可以为空(nil),如果一个变量没有声明为optional,那么必须给它一个确切的值;

  3. 类型转换
    需要指定转换类型:Sometype(val)

  4. 运算符
    与其他语言有相似的规则
    特殊规则

    • 可以对Float求余,8 % 2.5 // 等于 0.5;
    • 空值运算符 a ?? b,对可选类型a进行空判断,如果a包含一个值就进行解封,否则就返回一个默认值b,表达式a必须是Optional类型,默认值b的类型必须要和a存储值的类型保持一致
    • 区间运算符 闭区间运算符(a...b); 半开区间(a..<b
  5. 字符串
    Swift 的String类型是值类型。 如果您创建了一个新的字符串,那么当其进行常量、变量赋值操作或在函数/方法中传递时,会进行值拷贝。 任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值操作。

  6. 数组

var shoppingList: [String] = ["Eggs","Milk"]
shoppingList.append("Flour")
shoppingList += ["Chocolate Spread","Cheese","Butter"]
shoppingList[0] = "Six eggs"
shoppingList.insert("Maple Syrup",atIndex: 0)
let mapleSyrup = shoppingList.removeAtIndex(0)
for item in shoppingList {
    println(item)
}
  1. 字典
var airports: [String:String] = ["TYO": "Tokyo","DUB": "Dublin"]
for (airportCode,airportName) in airports {
    println("\(airportCode): \(airportName)")
}
for airportCode in airports.keys {
    println("Airport code: \(airportCode)")
}
// Airport code: TYO
// Airport code: LHR

for airportName in airports.values {
    println("Airport name: \(airportName)")
}
// Airport name: Tokyo
// Airport name: London Heathrow
let airportCodes = Array(airports.keys)
// airportCodes is ["TYO","LHR"]

let airportNames = Array(airports.values)
// airportNames is ["Tokyo","London Heathrow"]

数组和字典都是在单个集合中存储可变值。如果我们创建一个数组或者字典并且把它分配成一个变量,这个集合将会是可变的。这意味着我们可以在创建之后添加更多或移除已存在的数据项来改变这个集合的大小。与此相反,如果我们把数组或字典分配成常量,那么它就是不可变的,它的大小不能被改变。

  1. for 循环
let names = ["Anna","Alex","Brian","Jack"]
for name in names {
    println("Hello,\(name)!")
}

let numberOfLegs = ["spider": 8,"ant": 6,"cat": 4]
for (animalName,legCount) in numberOfLegs {
    println("\(animalName)s have \(legCount) legs")
}

for var index = 0; index < 3; ++index {
    println("index is \(index)")
}
  1. while、do while
while condition {
    statements
}

do {
    statements
} while condition
  1. if
var temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
    println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
    println("It's really warm. Don't forget to wear sunscreen.")
} else {
    println("It's not that cold. Wear a t-shirt.")
}
  1. switch
let someCharacter: Character = "e"
switch someCharacter {
case "a","e","i","o","u":
    println("\(someCharacter) is a vowel")
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
    println("\(someCharacter) is a consonant")
default:
    println("\(someCharacter) is not a vowel or a consonant")
}

这里default语句是必须的,否则编译不通过;不需要break,因为只会匹配一种情况。

case 中还可以加条件判断where

let yetAnotherPoint = (1,-1)
switch yetAnotherPoint {
case let (x,y) where x == y:
    println("(\(x),\(y)) is on the line x == y")
case let (x,y) where x == -y:
    println("(\(x),\(y)) is on the line x == -y")
case let (x,y):
    println("(\(x),\(y)) is just some arbitrary point")
}
// 输出 "(1,-1) is on the line x == -y"
  1. continue、break、fallthrough
    continue、break与其他语言用法相同;
    fallthrough用在switch语句中,实现条件贯穿,相当于Java或者其他语言case块中没有写break的情况:
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number,and also"
    fallthrough
default:
    description += " an integer."
}
println(description)
// 输出 "The number 5 is a prime number,and also an integer."

猜你在找的Swift相关文章