• 为什么有时候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转换成字符串.

  • 相关阅读:
    2018福大软工实践第六次作业
    2018福大软工实践第五次作业
    2018福大软工实践第四次作业
    2018福大软工实践第三次作业
    2018福大软工实践第二次作业
    团队现场编程实战(抽奖系统)
    团队Alpha版本冲刺(三)
    团队Alpha版本冲刺(二)
    团队Alpha版本冲刺(一)
    福大软工 · 第八次作业
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3946032.html
Copyright © 2020-2023  润新知