iOS 基本数据类型之NSData
1 nsdata
作用:
用于存储二进制的数据类型
nadat类提供一种简单的方式,它用来设置缓存区。将文件的内容读入到缓存区。或者将缓存区中的内容写到一个文件。
不变缓存区(nsdata类) 也可定义可变缓存区类(nsmutableData类)

NSData 、 NSString互转:
    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding]; //NSString转换成NSData类型
    NSString * newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSData 、Byte 互转
    NSString *testString = @"1234567890";
    NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
    Byte *testByte = (Byte *)[testData bytes];
    Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
    NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
  NSData转UIImage
    UIImage *aimage = [UIImage imageWithData: data];
  NSData转NSDictionary
@interface NSDictionary (Helpers)
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data;
@end
@implementation NSDictionary (Helpers)
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data {
CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data,
kCFPropertyListImmutable,
NULL);
if(plist == nil) return nil;
if ([(id)plist isKindOfClass:[NSDictionary class]]) {
return [(NSDictionary *)plist autorelease];
}
else {
CFRelease(plist);
return nil;
}
}
@end
NSData 应用场景
将文件的内容读入内存缓冲区
    NSData *fileData;
    NSFileManager *fileManager = [[NSFileManager alloc]init];
    fileData = [fileManager contentsAtPath:path];
  将缓冲区的内容写入到一个文件中
    NSFileManager *fileManager = [[NSFileManager alloc]init];
    [fileManager createFileAtPath:path contents:fileData attributes:nil];
天下文章一大抄 该文内容根据以下博客整理
http://www.cnblogs.com/GISerYang/p/3345350.html
http://bj007.blog.51cto.com/1701577/538990/
http://blog.csdn.net/jerryvon/article/details/7350567
http://www.cnblogs.com/duchengdong/archive/2012/03/29/2422997.html
http://www.myexception.cn/operating-system/1808379.html

iOS NSData简单解析的更多相关文章

  1. iOS开发-简单解析JSON数据

    什么是JSON   JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典和数组   {“nam ...

  2. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  3. 2016 - 1- 23 iOS中xml解析 (!!!!!!!有坑要解决!!!!!!)

    一: iOS中xml解析的几种方式简介 1.官方原生 NSXMLParser :SAX方式解析,使用起来比较简单 2.第三方框架 libxml2 :纯C 同时支持DOM与SAX GDataXML: D ...

  4. 对 cloudwu 简单的 cstring 进行简单解析

    题外话 以前也用C写过字符串,主要应用的领域是,大字符串,文件读取方面.写的很粗暴,用的凑合着.那时候看见云风前辈的一个开源的 cstring 串. 当时简单观摩了一下,觉得挺好的.也没细看.过了较长 ...

  5. iOS push全方位解析(二)【译文】"——生成OpenSSL证书,Provisioning Profile

    这是一篇来自raywenderlich的教程,内容翔实!结构简单透彻.讲解循序渐进.文章质量上乘!是一篇难的的博文!使用半瓶的英语水平翻译了一下: 1.[iOS push全方位解析](一) push的 ...

  6. (转)iOS Wow体验 - 第二章 - iOS用户体验解析(2)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第二章译文精选的第二部分,其余章节将陆续放出.上一 ...

  7. iOS开发 XML解析和下拉刷新,上拉加载更多

    iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...

  8. iOS使用TFHpple解析html

    iOS 开发中解析html 网上有很多写好的解析框架 今天就来讲一下如何用框架TFHpple来解析html 使用TFHpple解析html github地址:https://github.com/to ...

  9. iOS上简单推送通知(Push Notification)的实现

    iOS上简单推送通知(Push Notification)的实现 根据这篇很好的教程(http://www.raywenderlich.com/3443/apple-push-notification ...

随机推荐

  1. Gitolite 构建 Git 服务器

    Gitolite 构建 Git 服务器 Gitolite 构建 Git 服务器作者: 北京群英汇信息技术有限公司网址: http://www.ossxp.com/版本: 0.1-1日期: 2010-1 ...

  2. 字符串(后缀自动机):Ahoi2013 差异

    Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...

  3. poj 2151

      http://poj.org/problem?id=2151                                                               Check ...

  4. removeTask

    SystemUI中,Home键调出小刷子杀最近任务,整个流程从其RecentsPanelView.java开始: public void handleSwipe(View view) { ... // ...

  5. BlogEngine.Net

    BlogEngine.Net架构与源代码分析系列part1:开篇介绍 2008-11-05 15:27 by GUO Xingwang, ...阅读, ...评论, 收藏, 编辑 最近我要开始这个系列 ...

  6. HDOJ 1081(ZOJ 1074) To The Max(动态规划)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  7. IronPython fail to add reference to WebDriver.dll

    在使用Ironpython引用WebDriver程序集做web自动化时碰到这个问题,出问题的代码很简单,如下: import sys import clr clr.AddReferenceToFile ...

  8. 实现自己的脚本语言ngscript之四:代码生成

    最近的进度 ngscript测试代码 function c1(a, b, c, d) { this.a = 1; this.b = new array(); this.b[0] = 1; this.b ...

  9. java 23 种设计模式

    一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接 ...

  10. EXCEL VBA运行不显示系统提示

    Sub XXX() Application.DisplayAlerts = False '代码段 Application.DisplayAlerts = True End Sub 以下是MICROSO ...