獨立文件放在工程下即可
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!!!"}
引用地