swift调用自定义类库

独立文件放在工程下即可

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!!!"}

引用地

Swift Classes – Part 1


This entry was posted in Xcode and tagged , , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/class-swift/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.
2020.03.24 评论已经全局关闭,有事加QQ聊天