如何在Swift中初始化NSData中的字符串 - How to initialise a string from NSData in Swift

問題:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章