为什么有时候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转换成字符串.
为什么有时候NSData转换成NSString的时候返回nil的更多相关文章
- 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动操作使得start可以转换成end时, 返回True。
在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作.一次移动操作指用一个"LX"替换一个"XL ...
- 字典转换成NSString(NSJson)
//字典转换成字符串 NSDictionary *dict = [NSMutableDictionary dictionary]; NSData *data = [NSJSONSerializatio ...
- iOS中NSString转换成HEX(十六进制)-NSData转换成int
http://www.2cto.com/kf/201402/281501.html 1 2 3 4 5 6 NSString *str = @"0xff055008"; //先以1 ...
- 字符串转换成NSDate类型的 为nil解决方法
方法一 通过下列函数来解决 但是得到的日期会改变 修改方法fix - (NSDate *)timeForString:(NSString *)string { NSMut ...
- NSString 和NSData 转换
NSString 转换成NSData 对象 NSData* xmlData =[@"testdata" dataUsingEncoding:NSUTF8StringEncoding ...
- iOS NSString 和NSData 转换
NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...
- NSString 和 NSData 转换
NSString 转换成NSData 对象 NSData* xmlData =[@"testdata" dataUsingEncoding:NSUTF8StringEncoding ...
- 解决NSData转NSString返回nil的问题
// 字符串转Data NSString *str =@"jesfds"; NSData *data =[str dataUsingEncoding:NSUTF8StringEnc ...
- java基础60 JavaScript字符串转换成数字(网页知识)
1.字符串转换成数字 <!doctype html> <html> <head> <meta charset="utf-8"> &l ...
随机推荐
- PM2怎么开启ES6?
node版本v6.14.2,无论是用配置文件 { "apps": [{ "name": "server", "script&quo ...
- SpringBoot入门 (二) 属性文件读取
在上一篇中介绍了在idea中创建springboot工程及使用web工程输出“helloworld”到前端页面,本文学习在springboot工程中读取属性文件中的属性值. 一 自定义属性 在appl ...
- Classpath entry org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER will not be exported or published
sometimes when importing a maven project in eclipse, i get the following error: Classpath entry org. ...
- https在电子邮件安全解决方案
电子邮件安全解决方案 电子邮件已经成为现代人最重要和最不可缺少的个人生活和工作的通信工具之一,特别是企业应用.但是,您也许不知道,所有电子邮件系统都是明文传输,也就是说:您的每一个重要邮件都是在以“明 ...
- golang基础--控制语句
go基础之控制语句 补充知识 指针 与其他语言不同,在Go中不支持指针运算即->运算符,而直接采用.选择符来操作指针目标对象的成员. 操作符&取变量的地址,使用*通过指针间间接访问目标对 ...
- MongoDB之集合管理二
上一博客写了集合的管理,集合里面存放的是文档,因此聪明的你应该能想到这篇是学习文档管理.要说标题应该是文档管理,不过对于文档的管理都是先获得集合对象,在集合对象上调用方法管理文档,所以标题还是对集合的 ...
- tcpdump一个命令的剖析
简单介绍:用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据 ...
- 监测代码执行时间之Stopwatch
var sw = new System.Diagnostics.Stopwatch(); sw.Start(); 中间为要执行的代码 sw.Stop(); var msg = "上面操作耗时 ...
- 通向全栈之路——(3)node环境搭建
1:更新系统 sudo apt-get update2:安装相关软件 sudo apt-get install vim openssl build-essential libssl-dev wget ...
- 项目托管到Github上
一.注册github账号 首先需要注册一个github账号,注册地址:https://github.com 接着会来到这 然后会收到一封github发的邮件,进入邮箱验证 二.创建个人的githu ...