Why does nil be returned when nsdata is converted to nsstring?
Sometimes, nsdata clearly has a value, but when converted to nsstring, there is no value. Now let's test :)
-Materials for testing are now available-
The source code is as follows:
//// Appdelegate. M // testnsdata /// created by youxianming on 14-8-30. // copyright (c) 2014 youxianming. all rights reserved. // # import "appdelegate. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {// obtain bundle path nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "youxianming.png" oftype: Nil]; // obtain the file nsdata * Data = [nsdata datawithcontentsoffile: path]; // print the file length nslog (@ "data. length = % lu ", (unsigned long) data. length); Return yes;} @ end
The output is as follows:
07:47:16. 146 testnsdata [1382: 60b] cannot find executable for cfbundle 0x8e5dfe0 </applications/xcode. APP/contents/developer/platforms/iphonesimulator. platform/developer/sdks/iphonesimulator7.1.sdk/system/library/accessibilitybundles/certuiframework. axbundle> (not loaded)
07:47:16. 193 testnsdata [1382: 60b] data. Length = 1210569
Convert nsdata to nsstring :)
//// Appdelegate. M // testnsdata /// created by youxianming on 14-8-30. // copyright (c) 2014 youxianming. all rights reserved. // # import "appdelegate. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {// obtain bundle path nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "youxianming.png" oftype: Nil]; // obtain the file nsdata * Data = [nsdata datawithcontentsoffile: path]; // print the file length nslog (@ "data. length = % lu ", (unsigned long) data. length); // convert nsdata to the string nsstring * datastr = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding]; nslog (@ "datastr = % @", datastr ); return yes;} @ end
The output is as follows:
08:02:30. 617 testnsdata [1459: 60b] cannot find executable for cfbundle 0x99add90 </applications/xcode. APP/contents/developer/platforms/iphonesimulator. platform/developer/sdks/iphonesimulator7.1.sdk/system/library/accessibilitybundles/certuiframework. axbundle> (not loaded)
08:02:30. 800 testnsdata [1459: 60b] data. Length = 1210569
08:02:30. 801 testnsdata [1459: 60b] datastr = (null)
Note: The print above is null :)
The following is an explanation:
You can't convert an uiimage to nsstring by using initwithdata: encoding: method. This method is only for converting an string's data to nsstring (an text file for example ).
If you are trying to convert any kind of binary data to nsstring, there are some kind of encoding available. base64 is widely used.
You cannot convert a uiimage by using initwithdata: encoding.This method can only convert data in character format(For example, a text file ).
If you try to convert any type of nsdata to nsstring, there are many encodings available for you to choose from. base64 is the most widely used.
Conclusion:
You can only convert nsdata in character format to a string.
Why does nil be returned when nsdata is converted to nsstring?