Objective-C Convert NSData to NSString

后端 未结 3 1883
长情又很酷
长情又很酷 2021-01-18 16:50

Here are the code I tried to convert NSData to NSString but the program return \"Program received signal:SIGABRT\".

NSString *string= [NSString stringWithUTF         


        
相关标签:
3条回答
  • 2021-01-18 17:01

    Use this code

    NSString* myString;
    myString = [[NSString alloc] initWithData:nsdata encoding:NSASCIIStringEncoding];
    

    also see this tutorial..

    http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

    :)

    0 讨论(0)
  • 2021-01-18 17:05

    Here's a highly up-voted answer that shows how to do it. In a nutshell:

    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

    The first idea looks like a fail because of sending [data bytes]. stringWithUTF8String isn't prepared for a void *. The second idea looks as if it should work, even with nil input.

    0 讨论(0)
  • 2021-01-18 17:06

    Swift-3

    let string = data.base64EncodedString()

    0 讨论(0)
提交回复
热议问题