extension Array where Element 代码学习

var fieldNames: [String] {

        let p = UnsafePointer<Int32>(self.pointer)

        return Array(utf8Strings: relativePointer(base: p.advanced(by: 3), offset: self.pointer.pointee.fieldNames))

    }

 

间接类型推断?! 

---------------------------------------

 

protocol UTF8Initializable {

    init?(validatingUTF8: UnsafePointer<CChar>)

}

 

extension String : UTF8Initializable {}

 

extension Array where Element : UTF8Initializable {

 

    init(utf8Strings: UnsafePointer<CChar>) {

        var strings = [Element]()

        var pointer = utf8Strings

        while let string = Element(validatingUTF8: pointer) {

            strings.append(string)

            while pointer.pointee != 0 {

                pointer.advance()

            }

            pointer.advance()

            guard pointer.pointee != 0 else {

                break

            }

        }

        self = strings

    }

}

posted @ 2018-06-19 19:34  zzfx  阅读(238)  评论(0编辑  收藏  举报