Swift 快速参考

本文是苹果Swift编程语言的备忘单和参考之南,以后会涵盖Swift的所有关键特性,包括Strings、Arrays、Dictionaries以及Flow Control。Swift是苹果在WWDC 2014上发布的适用于iOS和OS X平台应用的开发。(持续更新的内容,欢迎你来贴自己的备忘单)

[b]Variables[/b]
var myInt = 1 
var myExplicitInt: Int = 1 // explicit type
var x = 1, y = 2, z = 3 // declare multiple integers
myExplicitInt = 2 // set to another integer value


[b]Constants[/b]
let myInt = 1 
myInt = 2 // compile-time error!


[b]Strings[/b]
var myString = "a" 
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error!

let count = 7
let message = "There are \(count) days in a week"


[b]Logical Operators[/b]
var happy = true 
var sad = !happy // logical NOT, sad = false
var everyoneHappy = happy && sad // logical AND, everyoneHappy = false
var someoneHappy = happy || sad // logical OR, someoneHappy = true


[b]Printing[/b]
let name = "swift" 
println("Hello")
println("My name is \(name)")
print("See you ")
print("later")
/* Hello
My name is swift
See you later */


[b]Arrays[/b]
var colors = ["red", "blue"] 
var moreColors: String[] = ["orange", "purple"] // explicit type
colors.append("green") // [red, blue, green]
colors += "yellow" // [red, blue, green, yellow]
colors += moreColors // [red, blue, green, yellow, orange, purple]

var days = ["mon", "thu"]
var firstDay = days[0] // mon
days.insert("tue", atIndex: 1) // [mon, tue, thu]
days[2] = "wed" // [mon, tue, wed]
days.removeAtIndex(0) // [tue, wed]


[b]Dictionaries[/b]
var days = ["mon": "monday", "tue": "tuseday"] 
days["tue"] = "tuesday" // change the value for key "tue"
days["wed"] = "wednesday" // add a new key/value pair

var moreDays: Dictionary = ["thu": "thursday", "fri": "friday"]
moreDays["thu"] = nil // remove thu from the dictionary
moreDays.removeValueForKey("fri") // remove fri from the dictionary


[b]Conditionals[/b]
//IF STATEMENT 
let happy = true
if happy {
println("We're Happy!")
} else {
println("We're Sad :('")
}
// We're Happy!

let speed = 28
if speed <= 0 {
println("Stationary")
} else if speed <= 30 {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed


//SWITCH STATEMENT
let n = 2
switch n {
case 1:
println("It's 1!")
case 2...4:
println("It's between 2 and 4!")
case 5, 6:
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!


[b]For Loops[/b]
for var index = 1; index < 3; ++index { 
// loops with index taking values 1,2
}
for index in 1..3 {
// loops with index taking values 1,2
}
for index in 1...3 {
// loops with index taking values 1,2,3
}

let colors = ["red", "blue", "yellow"]
for color in colors {
println("Color: \(color)")
}
// Color: red
// Color: blue
// Color: yellow

let days = ["mon": "monday", "tue": "tuesday"]
for (shortDay, longDay) in days {
println("\(shortDay) is short for \(longDay)")
}
// mon is short for monday
// tue is short for tuesday



[b]While Loops[/b]
var count = 1 
while count < 3 {
println("count is \(count)")
++count
}
// count is 1
// count is 2

count = 1
while count < 1 {
println("count is \(count)")
++count
}
//


count = 1
do {
println("count is \(count)")
++count
} while count < 3
// count is 1
// count is 2

count = 1
do {
println("count is \(count)")
++count
} while count < 1
// count is 1



另附上raywenderlich上Ray Wenderlich的备忘单。

[img]http://dl2.iteye.com/upload/attachment/0097/9352/0115c529-4d80-3b5d-837f-96dfc89e7ddc.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值