• 为什么有时候NSData转换成NSString的时候返回nil


    为什么有时候NSData转换成NSString的时候返回nil

    有时候,NSData明明有值,可是,当转换成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 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // 获取bundle路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"YouXianMing.png"
                                                         ofType:nil];
        
        // 获取文件
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        // 打印文件长度
        NSLog(@"data.length = %lu", (unsigned long)data.length);
        
        return YES;
    }
    
    @end

    打印结果如下:

    2014-08-30 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)

    2014-08-30 07:47:16.193 TestNSData[1382:60b] data.length = 1210569

    我们把NSData转换成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 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // 获取bundle路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"YouXianMing.png"
                                                         ofType:nil];
        
        // 获取文件
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        // 打印文件长度
        NSLog(@"data.length = %lu", (unsigned long)data.length);
        
        // 将NSData转换为字符串
        NSString *dataStr = [[NSString alloc] initWithData:data
                                                  encoding:NSUTF8StringEncoding];
        
        NSLog(@"dataStr = %@", dataStr);
        
        return YES;
    }
    
    @end

    打印结果如下:

    2014-08-30 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)

    2014-08-30 08:02:30.800 TestNSData[1459:60b] data.length = 1210569

    2014-08-30 08:02:30.801 TestNSData[1459:60b] dataStr = (null)

    注意,上面的打印是null哦:)

    以下给出解释:

    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.

    你不能够将UIImage通过方法initWithData:encoding:来转换.这个方法只能够转换字符格式的data(例如文本格式的文件).

    如果你尝试着将任意类型的NSData转换成NSString,有许多编码可供你选择.Base64是使用最广泛的.

    结论:

    你只能够将字符格式的NSData转换成字符串.

  • 相关阅读:
    struts2之OGNL和struts2标签库和ValueStack对象
    struts2使用拦截器完成登陆显示用户信息操作和Struts2的国际化
    struts2的文件上传和文件下载
    jsp+servlet实现文件的上传和下载
    Ajax和JSON完成二级菜单联动的功能
    jxl的使用总结(java操作excel)
    Ajax异步验证登陆或者注册
    mydate97时间控件的使用
    Ckeditor一种很方便的文本编辑器
    搬家通知博文地址(将博客搬到CSDN)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3946032.html
Copyright © 2020-2023  润新知