NSData from NSKeyedArchiver to NSString

前端 未结 3 1107
悲&欢浪女
悲&欢浪女 2021-02-08 20:20

I\'m trying to convert NSData generated from NSKeyedArchiver to an NSString so that I can pass it around and eventually convert it back to NSData. I have to pass this as a strin

3条回答
  •  没有蜡笔的小新
    2021-02-08 21:07

    What you want is:

    id obj;
    
    NSData * data     = [NSKeyedArchiver archivedDataWithRootObject:obj];
    NSString * string = [data base64EncodedString];
    

    And then the other way around

    NSString * string;
    
    NSData * data    = [NSData dataFromBase64String:string];
    id obj = [NSKeyedUnarchiver unarchiveObjectWithData:data]
    

    You can add base64EncodedString and dataFromBase64String: with the NSData category available here NSData+Base64 but it is now included by default

提交回复
热议问题