Why does nil be returned when nsdata is converted to nsstring?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.