简体   繁体   中英

What the correct syntax for StringWithData?

Objective-C:

- (NSString *)stringWithData:(NSData *)data
{
    NSString *result = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
    return result;
}

Swift Pattern:

convenience init(bytes bytes: UnsafePointer<Void>, length length: Int, encoding encoding: UInt)

My interpretation:

func stringWithData(data:NSData) -> String {
        let result = NSString.(data:data, length:data.length, encoding:NSUTF8StringEncoding)
    }

I'm getting a compiler error: "Expectant member name followed by '.'"

What am I missing (I'm suspecting it has to do with the 1st parameter)?

Remove the . after NSString in your let result = ... line.

From Playground:

let data = "test".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)! as NSData
// NSConcreteMutableData
let result = NSString(data: data, encoding: NSUTF8StringEncoding) // "test"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM