Convert the UTF 8 encoded NSData to string in swift and objective c

Solution:

Use the below technics to convert the UTF 8 NsData to string in objective code swift.

Objective c:
Method 1:


        NSString* Str = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

Method 2:


        NSString* Str = [NSString stringWithUTF8String:[theData bytes]];



Swift:

Method 1:


        let Str = String(data: data, encoding: .utf8)

Method 2:

        let Str = String(data: data.subdata(in: 0 ..< data.count - 1), encoding: .utf8)

Method 3:


        let Str = data.withUnsafeBytes(String.init(utf8String:))

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift