ios开发之NSData
<div class="cnblogs_code"><pre><span style="color: rgba(0, 0, 0, 1)">NSData用于保存字节数组。
初始化
</span>- (instancetype)initWithBytesNoCopy:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;</span><span style="color: rgba(0, 0, 0, 1)">
初始化对象。
不进行复制字节数组操作,直接设置字节指针为bytes,长度为length。
</span>- (instancetype)initWithBytesNoCopy:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length;</span><span style="color: rgba(0, 0, 0, 1)">
初始化对象。
不进行复制字节数组操作,直接设置字节指针为bytes,长度为length。
</span>- (instancetype)initWithBytes:(nullable <span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length;</span><span style="color: rgba(0, 0, 0, 1)">
初始化对象。
复制字节数组,设置字节指针指向复制的字节数组,长度为length。
</span>- (nullable instancetype)initWithContentsOfFile:(NSString *<span style="color: rgba(0, 0, 0, 1)">)path;
</span><span style="color: rgba(0, 0, 0, 1)">
读取文件内容初始化对象。
读取成功则返回对象,如果失败则返回nil。
</span>- (nullable instancetype)initWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **<span style="color: rgba(0, 0, 0, 1)">)errorPtr;
</span><span style="color: rgba(0, 0, 0, 1)">
读取文件内容初始化对象。
读取成功则返回对象。如果失败则返回nil,错误信息保存在errorPtr中。
参数readOptionsMask 指定文件读取选项。
typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) {
NSDataReadingMappedIfSafe </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataReadingUncached </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataReadingMappedAlways</span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataReadingMapped </span>=<span style="color: rgba(0, 0, 0, 1)"> NSDataReadingMappedIfSafe,
NSMappedRead </span>=<span style="color: rgba(0, 0, 0, 1)"> NSDataReadingMapped,
NSUncachedRead </span>=<span style="color: rgba(0, 0, 0, 1)"> NSDataReadingUncached
};</span>
- (nullable instancetype)initWithContentsOfURL:(NSURL *<span style="color: rgba(0, 0, 0, 1)">)url;</span><span style="color: rgba(0, 0, 0, 1)">
读取url内容初始化对象。
读取成功则返回对象,如果失败则返回nil。
</span>- (nullable instancetype)initWithContentsOfURL:(NSURL *)url options:(NSDataReadingOptions)readOptionsMask error:(NSError **<span style="color: rgba(0, 0, 0, 1)">)errorPtr;</span><span style="color: rgba(0, 0, 0, 1)">
读取url内容初始化对象。
读取成功则返回对象。如果失败则返回nil,错误信息保存在errorPtr中。
参数readOptionsMask 指定文件读取选项。
</span>- (instancetype)initWithData:(NSData *<span style="color: rgba(0, 0, 0, 1)">)data;
</span><span style="color: rgba(0, 0, 0, 1)">
根据NSData对象初始化对象。
</span>- (nullable <span style="color: rgba(0, 0, 255, 1)">id</span>)initWithContentsOfMappedFile:(NSString *<span style="color: rgba(0, 0, 0, 1)">)path</span><span style="color: rgba(0, 0, 0, 1)">
根据文件内容初始化对象。读取文件内容的方式不是read系统调用,而是mmap系统调用。
构造
</span>+<span style="color: rgba(0, 0, 0, 1)"> (instancetype)data;</span><span style="color: rgba(0, 0, 0, 1)">
构造空的NSData对象。
</span>+ (instancetype)dataWithBytesNoCopy:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
</span><span style="color: rgba(0, 0, 0, 1)">
根据字节数组构造对象。不复制字节数组。
</span>+ (instancetype)dataWithBytesNoCopy:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length;
</span><span style="color: rgba(0, 0, 0, 1)">
根据字节数组构造对象。不复制字节数组。
</span>+ (instancetype)dataWithBytes:(nullable <span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length;
</span><span style="color: rgba(0, 0, 0, 1)">
根据字节数组构造对象。复制字节数组。
</span>+ (nullable instancetype)dataWithContentsOfFile:(NSString *<span style="color: rgba(0, 0, 0, 1)">)path;
</span><span style="color: rgba(0, 0, 0, 1)">
根据文件内容构造对象。
</span>+ (nullable instancetype)dataWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **<span style="color: rgba(0, 0, 0, 1)">)errorPtr;
</span><span style="color: rgba(0, 0, 0, 1)">
根据文件内容构造对象。
</span>+ (nullable instancetype)dataWithContentsOfURL:(NSURL *<span style="color: rgba(0, 0, 0, 1)">)url;
</span><span style="color: rgba(0, 0, 0, 1)">
根据url内容构造对象。
</span>+ (nullable instancetype)dataWithContentsOfURL:(NSURL *)url options:(NSDataReadingOptions)readOptionsMask error:(NSError **<span style="color: rgba(0, 0, 0, 1)">)errorPtr;
</span><span style="color: rgba(0, 0, 0, 1)">
根据url内容构造对象。
</span>+ (instancetype)dataWithData:(NSData *<span style="color: rgba(0, 0, 0, 1)">)data;
</span><span style="color: rgba(0, 0, 0, 1)">
根据NSData对象构造对象。
</span>+ (nullable <span style="color: rgba(0, 0, 255, 1)">id</span>)dataWithContentsOfMappedFile:(NSString *<span style="color: rgba(0, 0, 0, 1)">)path
</span><span style="color: rgba(0, 0, 0, 1)">
根据文件内容构造对象。读取文件内容的方式不是read系统调用,而是mmap系统调用。
返回长度
@property (</span><span style="color: rgba(0, 0, 255, 1)">readonly</span><span style="color: rgba(0, 0, 0, 1)">) NSUInteger length;
</span><span style="color: rgba(0, 0, 0, 1)">
返回数据
@property (</span><span style="color: rgba(0, 0, 255, 1)">readonly</span>) <span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">bytes
</span><span style="color: rgba(0, 0, 0, 1)">
返回区间内的数据
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)getBytes:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)buffer range:(NSRange)range;
</span><span style="color: rgba(0, 0, 0, 1)">
参数buffer保存获取的数据,参数range指定获取数据的区间。
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)getBytes:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)buffer length:(NSUInteger)length;
</span><span style="color: rgba(0, 0, 0, 1)">
获取指定长度的数据。如果length大于数据长度,则使用数据长度作为指定长度。
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)getBytes:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)buffer
</span><span style="color: rgba(0, 0, 0, 1)">
获取所有数据。
截取数据
</span>- (NSData *<span style="color: rgba(0, 0, 0, 1)">)subdataWithRange:(NSRange)range;
</span><span style="color: rgba(0, 0, 0, 1)">
参数range指定截取区间。
是否相等
</span>- (BOOL)isEqualToData:(NSData *<span style="color: rgba(0, 0, 0, 1)">)other;
</span><span style="color: rgba(0, 0, 0, 1)">
比较数据是否相等。
写入文件
</span>- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)writeOptionsMask error:(NSError **<span style="color: rgba(0, 0, 0, 1)">)errorPtr;
</span><span style="color: rgba(0, 0, 0, 1)">
参数path指定文件路径。参数errorPtr在写入失败时保存出错信息。参数writeOptionsMask 表示写入文件时的可选项,可使用或运算符连接。其可能值为
typedef NS_OPTIONS(NSUInteger, NSDataWritingOptions) {
NSDataWritingAtomic </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataWritingWithoutOverwriting NS_ENUM_AVAILABLE(10_8, 6_0) </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataWritingFileProtectionNone NS_ENUM_AVAILABLE_IOS(4_0)</span>= <span style="color: rgba(128, 0, 128, 1)">0x10000000</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataWritingFileProtectionComplete NS_ENUM_AVAILABLE_IOS(4_0)</span>= <span style="color: rgba(128, 0, 128, 1)">0x20000000</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataWritingFileProtectionCompleteUnlessOpen NS_ENUM_AVAILABLE_IOS(5_0)</span>= <span style="color: rgba(128, 0, 128, 1)">0x30000000</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication NS_ENUM_AVAILABLE_IOS(5_0)</span>= <span style="color: rgba(128, 0, 128, 1)">0x40000000</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataWritingFileProtectionMask NS_ENUM_AVAILABLE_IOS(4_0)</span>= <span style="color: rgba(128, 0, 128, 1)">0xf0000000</span><span style="color: rgba(0, 0, 0, 1)">,
NSAtomicWrite </span>=<span style="color: rgba(0, 0, 0, 1)"> NSDataWritingAtomic
};</span><span style="color: rgba(0, 0, 0, 1)">
NSDataWritingAtomic 表示使用辅助文件完成原子操作。
NSDataWritingWithoutOverwriting 表示防止覆盖现有文件,不能与NSDataWritingAtomic 结合使用。
</span>- (BOOL)writeToFile:(NSString *<span style="color: rgba(0, 0, 0, 1)">)path atomically:(BOOL)useAuxiliaryFile;
</span><span style="color: rgba(0, 0, 0, 1)">
写入文件。参数path指定文件路径,参数useAuxiliaryFile使用辅助文件完成原子操作。
写入url
</span>- (BOOL)writeToURL:(NSURL *)url options:(NSDataWritingOptions)writeOptionsMask error:(NSError **<span style="color: rgba(0, 0, 0, 1)">)errorPtr;
</span><span style="color: rgba(0, 0, 0, 1)">
参数path指定url路径。参数errorPtr在写入失败时保存出错信息。参数writeOptionsMask 表示写入时的可选项,可使用或运算符连接。
</span>- (BOOL)writeToURL:(NSURL *<span style="color: rgba(0, 0, 0, 1)">)url atomically:(BOOL)atomically;
</span><span style="color: rgba(0, 0, 0, 1)">
写入url。参数path指定文件路径,参数atomically完成原子操作。
搜索
</span>- (NSRange)rangeOfData:(NSData *<span style="color: rgba(0, 0, 0, 1)">)dataToFind options:(NSDataSearchOptions)mask range:(NSRange)searchRange
</span><span style="color: rgba(0, 0, 0, 1)">
搜索数据。参数dataToFind为搜索的数据。参数searchRange为搜索的区间。参数mask 为搜索的方式。搜索方式可使用或运算符连接。
搜索方式有:
typedef NS_OPTIONS(NSUInteger, NSDataSearchOptions) {
NSDataSearchBackwards </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataSearchAnchored </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 0, 0, 1)">
NSDataSearchBackwards表示从后向前搜索。
NSDataSearchAnchored表示只是搜索头部或尾部(与NSDataSearchBackwards连用)。
与Base64编码相关
</span>- (nullable instancetype)initWithBase64EncodedString:(NSString *<span style="color: rgba(0, 0, 0, 1)">)base64String options:(NSDataBase64DecodingOptions)options
</span><span style="color: rgba(0, 0, 0, 1)">
解码字符串。options为解码方式。
typedef NS_OPTIONS(NSUInteger, NSDataBase64DecodingOptions) {
NSDataBase64DecodingIgnoreUnknownCharacters </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">
} NS_ENUM_AVAILABLE(10_9, 7_0);
</span><span style="color: rgba(0, 0, 0, 1)">
NSDataBase64DecodingIgnoreUnknownCharacters 表示忽略不知道的字符。
</span>- (NSString *<span style="color: rgba(0, 0, 0, 1)">)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options
</span><span style="color: rgba(0, 0, 0, 1)">
编码为字符串。参数options为编码方式。
typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
NSDataBase64Encoding64CharacterLineLength </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataBase64Encoding76CharacterLineLength </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataBase64EncodingEndLineWithCarriageReturn </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">,
NSDataBase64EncodingEndLineWithLineFeed </span>= <span style="color: rgba(128, 0, 128, 1)">1UL</span> << <span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)">,
} NS_ENUM_AVAILABLE(10_9, 7_0);
</span>
- (nullable instancetype)initWithBase64EncodedData:(NSData *<span style="color: rgba(0, 0, 0, 1)">)base64Data options:(NSDataBase64DecodingOptions)options
</span><span style="color: rgba(0, 0, 0, 1)">
解码数据。
</span>- (NSData *<span style="color: rgba(0, 0, 0, 1)">)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options
</span><span style="color: rgba(0, 0, 0, 1)">
编码数据。
</span>- (nullable <span style="color: rgba(0, 0, 255, 1)">id</span>)initWithBase64Encoding:(NSString *<span style="color: rgba(0, 0, 0, 1)">)base64String
</span><span style="color: rgba(0, 0, 0, 1)">
解码字符串。
</span>- (NSString *<span style="color: rgba(0, 0, 0, 1)">)base64Encoding
</span><span style="color: rgba(0, 0, 0, 1)">
编码为字符串。
NSMutableData用于保存可变字节数组。
返回数据
@property (</span><span style="color: rgba(0, 0, 255, 1)">readonly</span>) <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">mutableBytes
</span><span style="color: rgba(0, 0, 0, 1)">
返回长度
@property NSUInteger length;</span><span style="color: rgba(0, 0, 0, 1)">
初始化
</span>-<span style="color: rgba(0, 0, 0, 1)"> (nullable instancetype)initWithCapacity:(NSUInteger)capacity;
</span><span style="color: rgba(0, 0, 0, 1)">
根据容量大小初始化对象。
</span>-<span style="color: rgba(0, 0, 0, 1)"> (nullable instancetype)initWithLength:(NSUInteger)length;
</span><span style="color: rgba(0, 0, 0, 1)">
根据长度初始化对象。数组全部清空为0。
构造
</span>+<span style="color: rgba(0, 0, 0, 1)"> (nullable instancetype)dataWithCapacity:(NSUInteger)aNumItems;
</span><span style="color: rgba(0, 0, 0, 1)">
根据容量大小构造对象。
</span>+<span style="color: rgba(0, 0, 0, 1)"> (nullable instancetype)dataWithLength:(NSUInteger)length;
</span><span style="color: rgba(0, 0, 0, 1)">
根据长度构造对象。
添加
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)appendBytes:(<span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes length:(NSUInteger)length;
</span><span style="color: rgba(0, 0, 0, 1)">
添加数组。
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)appendData:(NSData *<span style="color: rgba(0, 0, 0, 1)">)other;</span><span style="color: rgba(0, 0, 0, 1)">
添加数据。
替换
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)replaceBytesInRange:(NSRange)range withBytes:(<span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)bytes;
</span><span style="color: rgba(0, 0, 0, 1)">
替换字节数组。
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)replaceBytesInRange:(NSRange)range withBytes:(nullable <span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)replacementBytes length:(NSUInteger)replacementLength;
</span><span style="color: rgba(0, 0, 0, 1)">
替换字节数组。参数replacementLength指定替换数组的长度。
增加长度
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)increaseLengthBy:(NSUInteger)extraLength;
</span><span style="color: rgba(0, 0, 0, 1)">
重置
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)resetBytesInRange:(NSRange)range;
</span><span style="color: rgba(0, 0, 0, 1)">
重置区间内数据为0。
设置
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)setData:(NSData *)data;</pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/Free-Thinker/p/11412165.html
頁:
[1]