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转换成字符串.

  • 相关阅读:
    Nginx如何配置Http、Https、WS、WSS?
    关于MySQL日志,我与阿里P9都聊了些啥?
    一文搞懂MySQL体系架构!!
    在业务高峰期拔掉服务器电源是一种怎样的体验?
    千万不要轻易尝试“熊猫烧香”,这不,我后悔了!
    从小白程序员到大厂高级技术专家我看过哪些书籍?
    聊一聊我在 B 站自学 Java 的经历吧
    计算机网络的 89 个核心概念
    MQTT 协议是个啥?这篇文章告诉你!
    1.5w字 + 24张图肝翻 TCP。
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3946032.html
Copyright © 2011-2022 走看看