Suche…


Bemerkungen

Nützliche Ressourcen

Apple-Dokumentation (NSData)

NSData.dataWithContentsOfFile ()

NSData.bytes

NSData-Objekte erstellen

Datei verwenden

Schnell

let data = NSData(contentsOfFile: filePath) //assuming filePath is a valid path

Ziel c

NSData *data = [NSData dataWithContentsOfFile:filePath]; //assuming filePath is a valid path

Verwenden eines String-Objekts

Schnell

let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding) //assuming string is a String object

Ziel c

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; //assuming string is a String object

Konvertierung von NSData in andere Typen

Zu String

Schnell

let string = String(NSString(data: data, encoding: NSUTF8StringEncoding)) //assuming data is a valid NSData object

Ziel c

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //assuming data is a valid NSData object
[string release];

Zum Array

Schnell

let array = data.bytes as! NSMutableArray //assuming data is a valid NSData object

Ziel c

NSMutableArray *array = (NSMutableArray *)[data bytes]; //assuming data is a valid NSData object

In Byte-Array

Schnell

let bytesArray = data.bytes as! UInt8 //assuming data is a valid NSData object

Ziel c

UInt8 *bytesArray = (UInt8 *)data.bytes; //assuming data is a valid NSData object

Konvertierung von NSData in HEX-String

NSData kann als hexadezimaler String dargestellt werden, ähnlich wie bei der description .

Schnell

extension NSData {

    func hexString() -> String {
        return UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(bytes), count: length)
            .reduce("") { $0 + String(format: "%02x", $1) }
    }

}

Ziel c

@implementation NSData (HexRepresentation)

- (NSString *)hexString {
    const unsigned char *bytes = (const unsigned char *)self.bytes;
    NSMutableString *hex = [NSMutableString new];
    for (NSInteger i = 0; i < self.length; i++) {
        [hex appendFormat:@"%02x", bytes[i]];
    }
    return [hex copy];
}

@end


Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow