一萌京、枚舉 enum (Int 雁歌、String)
** Swfit3 中 枚舉類型都是小寫,為了規(guī)范知残,盡量和系統(tǒng)保持一致**
// String 類型
enum TestIntType: Int {
case defaultType, twoTypeName, threeTypeName
}
// Int 類型
enum TestStringType: String {
case defaultType = "a", twoTypeName = "b", threeTypeName = "c"
}
二靠瞎、公有屬性和私有屬性
** 公有屬性:聲明一個view**
// 表示testView可以為nil
public var testView: UIView?
// 表示testView不能為nil ,必須有值
public var testView: UIView
** 私有屬性:聲明一個label**
private var testLabel: UILabel?
** 公有方法**
// 聲明一個公有方法求妹,默認是公有方法
func testMethod() {
// code
}
// 或者
public func testMethod() {
// code
}
私有方法
private func testMethod() {
// code
}
三乏盐、弱引用: weak的使用
// 協(xié)議: optional 代表可選協(xié)議
@objc protocol testDelegate {
@objc optional func test() -> Void
}
// weak 修飾協(xié)議
weak public var delegate: testDelegate?
// block中修飾self
let NotificationtTest: NSNotification.Name = NSNotification.Name(rawValue: "NotificationtTest")
// 通知
NotificationCenter.default.addObserver(forName: NotificationtTest, object: nil, queue: nil) { [weak self](notification) in // weak修飾self
// 寫需要的邏輯
}
四、if 和 guard 的區(qū)別
let a = 10
if a > 0 { // 條件成立則執(zhí)行語句
print(a)
}
var b: String? = nil
guard let c = b else { // 如果條件不成立則執(zhí)行語句
// 所以這里會執(zhí)行語句,因為 let c = b 不存在
}
// 即等同于
if b != nil {
} else { // 這里 else 等同于上面guard 條件判斷不成立
}
PS: 有什么問題歡迎留言制恍。