问题:
I have been trying to initialise a string from NSData
in Swift. 我一直在尝试从Swift中的NSData
初始化一个字符串。
In the NSString Cocoa Documentation Apple is saying you have to use this: 在NSString Cocoa Documentation中, Apple说你必须使用它:
init(data data: NSData!, encoding encoding: UInt)
However Apple did not include any example for usage or where to put the init
. 但是,Apple没有包含任何使用示例或在哪里放置init
。
I am trying to convert the following code from Objective-C to Swift 我试图将以下代码从Objective-C转换为Swift
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
I have been trying a lot of possible syntaxes such as the following (of course it did not work): 我一直在尝试很多可能的语法,例如以下(当然它不起作用):
var string:NSString!
string = init(data: fooData,encoding: NSUTF8StringEncoding)
解决方案:
参考一: https://en.stackoom.com/question/1cnXp参考二: https://stackoom.com/question/1cnXp