Variables

var myInt =
var myExplicitInt: Int = // explicit type
var x = , y = , z = // declare multiple integers
myExplicitInt = // set to another integer value

Constants

let myInt =
myInt = // compile-time error!

Strings

var myString = "a"
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error! let count =
let message = "There are \(count) days in a week"

Logical Operators

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

Printing

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

Arrays

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[] // mon
days.insert("tue", atIndex: ) // [mon, tue, thu]
days[] = "wed" // [mon, tue, wed]
days.removeAtIndex() // [tue, wed]

Dictionaries

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

Conditionals

//IF STATEMENT
let happy = true
if happy {
println("We're Happy!")
} else {
println("We're Sad :('")
}
// We're Happy! let speed =
if speed <= {
println("Stationary")
} else if speed <= {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed //SWITCH STATEMENT
let n =
switch n {
case :
println("It's 1!")
case ...:
println("It's between 2 and 4!")
case , :
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!

For Loops

for var index = ; index < ; ++index {
// loops with index taking values 1,2
}
for index in .. {
// loops with index taking values 1,2
}
for index in ... {
// 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

While Loops

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

Functions

func iAdd(a: Int, b: Int) -> Int {
return a + b
}
iAdd(, ) // returns 5 func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) {
return (n-, n+)
}
eitherSide() // returns the tuple (4,6)

Classes

class Counter {
var count: Int =
func inc() {
count++
}
func add(n: Int) {
count += n
}
func printCount() {
println("Count: \(count)")
}
} var myCount = Counter()
myCount.inc()
myCount.add()
myCount.printCount() // Count: 3

												

最新文章

  1. C++ ## ... 实用
  2. HDU-1233 还是畅通工程
  3. Volley(二)—— 基本Request对象 &amp; RequestQueue&amp;请求取消
  4. ocx在我indows7无法注册
  5. lambda表达式————一看就会
  6. .NET vs JAVA
  7. cocos2dx 3.1从零学习(六)——CocosStudio(VS2013project导入及环境设置)
  8. PLSQL的一些事儿
  9. ubuntu安装mysql可视化工具MySQL-workbench及简单操作
  10. CSS揭秘—灵活的背景图(三)
  11. Django2 Django MTV模板
  12. 搭建一个MP-demo(mybatis_plus)
  13. python提取浏览器Cookie
  14. jdk环境配置以及java执行过程
  15. 用户登录页面——jdbc
  16. windows系统的便签
  17. poj-------Common Subsequence(poj 1458)
  18. android中用studio更改包名
  19. python 获取当前时间(关于time()时间问题的重要补充)
  20. apache DOCUMENT_ROOT

热门文章

  1. adb不响应
  2. IOS知识小记
  3. cocos2d-x 将cocosbuilder输出文件映射成对象的原理
  4. oracle查找重复记录
  5. 【转】memcached工作原理介绍
  6. 【转】bt协议的详细分析
  7. Android中文乱码彻底解决
  8. [GIF] Parenting in GIF Loop Coder
  9. github fork项目后,代码更新
  10. 会话跟踪session cookie