如何将NSString值转换为NSData?
NSString
NSData
NSString* str = @"teststring";NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
做:
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
然后随意继续NSJSONSerialization:JSONObjectWithData。
NSJSONSerialization:JSONObjectWithData
在评论,官方留档和核查之后,这个答案更新了关于删除所谓的NULL终止符:
如数据使用编码所示:
返回值 以NO作为第二个参数调用dataUsingEncoding:allowLossyConversion:的结果块状报价>
以NO作为第二个参数调用dataUsingEncoding:allowLossyConversion:的结果
dataUsingEncoding:allowLossyConversion:
如获取C String: max长度:编码:和c使用编码的字符串:所示:
请注意,dataUsingEncoding:allowLossyConversion:返回的数据不是严格的C字符串,因为它没有NULL终止符块状报价>
请注意,dataUsingEncoding:allowLossyConversion:返回的数据不是严格的C字符串,因为它没有NULL终止符
首先,您应该使用dataUsingEncoding:而不是UTF8String。只有在该编码中需要C字符串时才使用UTF8String。
dataUsingEncoding:
UTF8String
C
然后,对于UTF-16,只需在dataUsingEncoding:消息中传递NSUnicodeStringEncoding而不是NSUTF8StringEncoding。
UTF-16
NSUnicodeStringEncoding
NSUTF8StringEncoding
如果Swift Developer来到这里,
从NSString/String转换为NSData
var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)
NSString *str = @"helowrld";// This converts the string to an NSData objectNSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
你可以参考这个链接
NSString *str = @"hello";NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];
Objective-C:
NSString *str = @"test string";NSData *data = [NSKeyedArchiver archivedDataWithRootObject:str];NSString *thatStr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Swift:
let str = "test string"let data = NSKeyedArchiver.archivedData(withRootObject: str)let thatStr = NSKeyedUnarchiver.unarchiveObject(with: data) as! String
对于Swift 3,您将主要从String转换为Data。
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];
Objective-c
NSString *str = @"Hello World";NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
Swift
let str = "Hello World"let data = string.data(using: String.Encoding.utf8, allowLossyConversion: false)
NSString到NSData:
NSString* str= @"string";NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSData到NSString:
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
字符串到数据:
var testString = "string"var somedata = testString.data(using: String.Encoding.utf8)
数据到字符串:
var backToString = String(data: somedata!, encoding: String.Encoding.utf8) as String!
更新Swift 5. x
let str = "teststring"let data = str.data(using: .utf8)
在Swift中,有一个API返回非可选
let str = "teststring"let data = Data(str.utf8)
Swift 5. x
let myStringToConvert = "My String to Convert in Data"let myData = myStringToConvert.data(using: .utf8)
var myStringToConvert = "My String to Convert in Data"var myData = myStringToConvert.data(using: String.Encoding.utf8)
var backToMyString = String(data: myData!, encoding: String.Encoding.utf8) as String!
目标C:
NSString* myStringToConvert= @"My String to Convert in Data";NSData* myData=[str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString* backToMyString = [[NSString alloc] initWithData: myData encoding:NSUTF8StringEncoding];