独立文件放在工程下即可
import Foundation
class TV{
//定义属性
var price = 1999
var name = "乐TV"
//定义方法
func open()->String{
println("i am open hha")
return "Success"
}
func open2(){
println("shit");
}
func close()->Bool{
return true
}
}
自定义的类库
调用方式
var s = TV()
s.open()
带自动初始化
class User {
var firstName: String
var lastName: String
var bio: String = "I ♡ Swift!"
// no bio provided
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
// bio provided
init(firstName: String, lastName: String, bio: String) {
self.firstName = firstName
self.lastName = lastName
self.bio = bio
}
}
var me = User(firstName: "Andrei", lastName: "Puni")
// me = { firstName: "Andrei", lastName: "Puni", bio: "I ♡ Swift!"}
var silviu = User(firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!")
// silviu = { firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!"}
引用地