目标-C:
- (NSString *)stringWithData:(NSData *)data
{
NSString *result = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
return result;
}
Swift模式:
convenience init(bytes bytes: UnsafePointer<Void>, length length: Int, encoding encoding: UInt)
我的解释是:
func stringWithData(data:NSData) -> String {
let result = NSString.(data:data, length:data.length, encoding:NSUTF8StringEncoding)
}
我收到编译错误:“预期成员名称后跟'。'”
我错过了什么(我怀疑它与第一个参数有关)?
答案 0 :(得分:1)
删除.
行中NSString
之后的let result = ...
。
答案 1 :(得分:1)
来自游乐场:
let data = "test".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)! as NSData
// NSConcreteMutableData
let result = NSString(data: data, encoding: NSUTF8StringEncoding) // "test"