2016-08-16 12 views
8

Kod şimdi Swift 3 aşağıdaki hatayı atıyor:'byte' kullanılamaz: Daha önce Swift 2.2 çalışıyordu kullanım withUnsafeBytes yerine

let tempData: NSMutableData = NSMutableData(length: 26)! 
tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes:data.bytes) 
: Burada

enter image description here

benim kodudur

Hatayı gidermek için "data.bytes" i ne değiştirmeliyim? 'WithUnsafeBytes' uygulamasını denedim ve Apple'ın belgelerine bir göz attım ama kafamı bulamadım!

+0

"Veri" kaynağını sağlamadınız, ancak bunu ayrıca "Veri" ye dönüştürebilirseniz, bu işlem çok daha basit olacaktır ve "NSMutableData" ile "Data" arasında köprü kurmanıza gerek yoktur. Sadece 'replaceSubrange' kullanacaksınız. –

cevap

9

dataData tip sahip olduğunu varsayarak, aşağıdaki çalışması gerekir:

let tempData: NSMutableData = NSMutableData(length: 26)! 
data.withUnsafeBytes { 
    tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes: $0) 
} 

Data arasında

/// Access the bytes in the data. 
/// 
/// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure. 
public func withUnsafeBytes<ResultType, ContentType>(_ body: @noescape (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType 

yöntemi kullanılarak. Kapananın içinde $0, baytlara (Xcode 8 beta 6'daki UnsafeRawPointer) bir UnsafePointer<Void> 'dur.

+0

hızlı 3.0 için yanıtı güncelleştirebilir –

+0

@TejasArdeshna: Bu * * Swift 3. –

+0

neden uzunluğu 26 olarak kodlanmış? Büyük veriler için herhangi bir sorun yaratır mı? Sadece onaylayan ios için yeniyim. –

İlgili konular