码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-30 08:44:29      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   io   strong   ar   for   

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

bubuko.com,布布扣

有时候,NSData明明有值,可是,当转换成NSString的时候,却没有值,现在来进行测试:)

-现在提供测试用素材-

bubuko.com,布布扣

源码如下:

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

 

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

标签:style   blog   http   color   使用   io   strong   ar   for   

原文地址:http://www.cnblogs.com/YouXianMing/p/3946032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!