Szukaj…
Uwagi
Tworzenie obiektów NSData
Korzystanie z pliku
Szybki
let data = NSData(contentsOfFile: filePath) //assuming filePath is a valid path
Cel C
NSData *data = [NSData dataWithContentsOfFile:filePath]; //assuming filePath is a valid path
Korzystanie z obiektu String
Szybki
let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding) //assuming string is a String object
Cel C
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; //assuming string is a String object
Konwertowanie NSData na inne typy
Do ciągu
Szybki
let string = String(NSString(data: data, encoding: NSUTF8StringEncoding)) //assuming data is a valid NSData object
Cel C
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //assuming data is a valid NSData object
[string release];
Do tablicy
Szybki
let array = data.bytes as! NSMutableArray //assuming data is a valid NSData object
Cel C
NSMutableArray *array = (NSMutableArray *)[data bytes]; //assuming data is a valid NSData object
Do tablicy bajtów
Szybki
let bytesArray = data.bytes as! UInt8 //assuming data is a valid NSData object
Cel C
UInt8 *bytesArray = (UInt8 *)data.bytes; //assuming data is a valid NSData object
Konwertowanie NSData na ciąg HEX
NSData
mogą być reprezentowane jako ciąg szesnastkowy, podobnie jak dane wyjściowe w metodzie description
.
Szybki
extension NSData {
func hexString() -> String {
return UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(bytes), count: length)
.reduce("") { $0 + String(format: "%02x", $1) }
}
}
Cel 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
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow