zoukankan      html  css  js  c++  java
  • 为什么有时候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转换成字符串.

  • 相关阅读:
    python3操作mysql数据库表01(封装查询单条、多条数据)
    python3操作mysql数据库表01(基本操作)
    Python3基础02(列表和字符串处理)
    Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )
    Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)
    Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)
    Python3+Selenium3+webdriver学习笔记11(cookie处理)
    Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)
    Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)
    Python3+Selenium3+webdriver学习笔记8(单选、复选框、弹窗处理)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3946032.html
Copyright © 2011-2022 走看看