Swift笔记 - 12.访问控制、初始化器、扩展、weak、unowned、Autorelease

Swift笔记 - 12.访问控制、初始化器、扩展、weak、unowned、AutoreleasePool

访问控制

访问控制级别的使用准则

public class PublicClass {
    public var p1 = 8 // public
    var p2 = 8 // internal
    fileprivate func f1() {} // fileprivate
    private func f2() {} // private
}

class InnerClass { // internal
    var p = 0 // internal
    fileprivate func f1() {} //fileprivate
    private func f2() {} //private
}

fileprivate class PilePrivateClass ( // fileprivate
    func 110) 0) / fileprivate
    private func 12() { // private
}
private class PrivateClass { // private
    func f() {} // private
}
fileprivate(set) public var nun = 10
class Person {
    private(set) var age = 0
    fileprivate(set) public var weight: Int
    set {}
    get { 10 }
    internal(set) public subscript(index: Int) -> Int (
    set {}
    get { index }
}

初始化器

扩展(Extension)

Extension的计算属性、下标、方法、嵌套类型

extension Double {
    var km: Double { self * 1 000.0 }
    var m: Double { self }
    var dm: Double $ self / 100.0
    var cm: Double { self / 10.0 }
    var mm: Double { self / 1 000.0 }
}

extension Array {
    subscript(nullable idx: Int) -> Element? {
        if (startIndex..<endIndex).contains(idx) {
            return self[idx]
        }
        return nil
    }
}

extension Int {
    func repetitions(task: () -> Void) {
        for _ in 0..<self { task() }
    }
    mutating func square() -> Int {
        self = self * self
        return self
    }
    enum Kind { case negative, zero, positive }
    var kind: Kind {
        switch self {
            case 0: return .zero
            case let x where x > 0: return .positive
            default: return .negative
        }
    }
    subscript(digitIndex: Int) -> Int {
        var decimalBase = 1
        for _ in 0..<digitIndex { decimalBase *= 10 }
        return (self / decimalBase) % 10
    }
}

协议、初始化器

class Person {
    var age: Int
    var name: String
    init(age: Int, name: String) {
        self.age = age
        self.name = name
    }
}
extension Person : Equatable {
    static func == (left: Person, right: Person) -> Bool {
        left.age == right.age && left.name == right.name
    }
    convenience init() {
        self.init(age: 0, name: "'")
    }
}

struct Point {
    var x: Int = 0
    var y: Int = 0
}
extension Point {
    init(_ point: Point) {
        self.init(x: point.x, y: point.y)
    }
}
var p1 = Point()
var p2 = Point(x: 10)
var p3 = Point(y: 20)
var p4 = Point(x: 10, y: 20)
var p5 = Point(p4)

使用扩展来遵守协议,也可以使用扩展来为协议添加方法实现

protocol TestProtocol {
    func test()
}
class TestClass {
    func test() {
        print("test")
    }
}
extension TestClass : TestProtocol {}
protocol TestProtocol {
    func test1()
}
extension TestProtocol {
    func test1() {
        print ("TestProtocol test1")
    }
    func test2() {
        print ("TestProtocol test2")
    }
}

class TestClass : TestProtocol {}
var cls = TestClass()
cls.test1() // TestProtocol test1
cls.test2() // TestProtocol test2
var cls2: TestProtocol = TestClass()
cls2.test1() // TestProtocol test1
cls2.test2() // TestProtocol test2
class TestClass : TestProtocol {}
func test1() { print ("TestClass test1") }
func test2() { print ("TestClass test2") }
var cls = TestClass()
cls.test1() // TestClass test1
cls.test2() // TestClass test2
var cls2: TestProtocol = TestClass()
cls2.test1() // TestClass test1
cls2.test2() // TestProtocol test2
extension BinaryInteger {
    func isOdd() -> Bool { self % 2 != 0 }
}
var age: Int8 = 10

print(age.isOdd())
print(10.is0dd())
print((-3).isodd())

弱引用

weak、unowned的使用限制

protocol Livable : AnyObject {}
class Person {}

weak var pO: Person?
weak var p1: AnyObject?
weak var p2: Livable?

unowned var p10: Person?
unowned var p11: AnyObject?
unowned var p12: Livable?

AutoreleasePool

public func autoreleasepool<Result>(invoking body: () throws -> Result) rethrows -> Result
autoreleasepool {
    let p = MJPerson(age: 20, name: "Jack")
    p.run()
}

版权所有,转载请注明出处 luowei.github.io.