問題:
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