swift調用自訂類庫的例子

來源:互聯網
上載者:User

獨立檔案放在工程下即可

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!!!"}
引用地

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.