Swift: Data/String Convert to UnsafePointer


extension String {
    var unsafePointer: UnsafePointer {
        return self.withCString { $0 }
    }
    
    var unsafeBufferPointer: UnsafeBufferPointer {
        var tmpStr = self
        return tmpStr.withUTF8 { $0 }
    }
}

extension Data {
    var unsafeRawBufferPointer: UnsafeRawBufferPointer {
        return self.withUnsafeBytes { $0 }
    }
    
    var unsafeMutableRawBufferPointer: UnsafeMutableRawBufferPointer {
        var data = self
        return data.withUnsafeMutableBytes { $0 }
    }
    
    var unsafeBufferPointer_UInt8: UnsafeBufferPointer? {
        return self.withContiguousStorageIfAvailable { $0 }
    }
    
    var UnsafeMutableBufferPointer_UInt8: UnsafeMutableBufferPointer? {
        var data = self
        return data.withContiguousMutableStorageIfAvailable { $0 }
    }
}

你可能感兴趣的:(Swift: Data/String Convert to UnsafePointer)