objective-c - array - objective c byte




如何將NSString值轉換為NSData? (7)

Objective-C的

NSString *str = @"Hello World";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

迅速

let str = "Hello World"
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: false)

如何將NSString值轉換為NSData


Objective-C的:

NSString *str = @"test string";
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:str];
NSString *thatStr = [NSKeyedUnarchiver unarchiveObjectWithData:data];

迅速:

let str = "test string"
let data = NSKeyedArchiver.archivedData(withRootObject: str)
let thatStr = NSKeyedUnarchiver.unarchiveObject(with: data) as! String

在Swift Developer來到這裡的情況下,

從NSString / String轉換為NSData

var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)

對於Swift 3,您將主要將String轉換為Data

let myString = "test"
let myData = myString.data(using: .utf8)
print(myData) // Optional(Data)

NSString *str = @"Banana";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:true];

NSString *str = @"hello";
NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];

NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];




nsdata