首页 > 代码库 > swift
swift
import Cocoa
var str = "Hello, playground" + " and hyt"
var i=10
var f=5.0
f=f+5.0
let fruits = ["apple", "banana", "orange", "cherry"]
for some in fruits {
println("\(some)")
some
}
fruits.count
var another = fruits.map({(some:String) -> String in return some+" 3"})
another
func sayHello(name:String) -> String {
return "Hello \(name)."
}
sayHello("hyt")
fruits.map(sayHello);
var sum = 0
for num in 1..100 {
sum += num
}
sum
func getfunc() -> ((Int, Int)->Int) {
func add(a:Int, b:Int)->Int{ return a+b}
return add
}
getfunc()(1,2)
var a = [1,2,3,"hyt",2.1]
a.count
a.className
class Phone {
var type=""
func what()->String{
return "I‘m iPhone\(type)."
}
}
var iPhone = Phone()
iPhone.type = "4S"
iPhone.what()
class Bala : Phone{
init(type:String){
super.init()
self.type=type
}
}
var bala = Bala(type:"5S")
bala.what()
protocol ExampleProtocol {
mutating func adjust()
}
extension Double: ExampleProtocol {
mutating func adjust(){
self+=1
}
}
var db: Double = 1.10
db.adjust()
db
// simple pattern matching
let (x, y) = (1,2)
x
var dd = x+1
let (_, z) = ("hyt", "bob")
z
str = "hyt/bob".uppercaseString
str.pathComponents
str = "HYT.BOB".lowercaseString
var names = str.componentsSeparatedByString(".")
names += "app"
names += ["Anna", "git"]
str = names[2]
names.isEmpty
names = names.reverse()
str = names.reduce(str, {(a:String, some:String)-> String in return a+","+some})
str
str = ""
names.reduce(str, +)
var nums = [1,2,3]
var num = 0;
nums.reduce(num, +)
来自:http://www.cocoachina.com/bbs/read.php?tid=204774