OC-NSData详解

NSData用于保存字节数组。
初始化

 

- (instancetype)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;

初始化对象。
不进行复制字节数组操作,直接设置字节指针为bytes,长度为length。

 

- (instancetype)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;

初始化对象。
不进行复制字节数组操作,直接设置字节指针为bytes,长度为length。

 

- (instancetype)initWithBytes:(nullable const void *)bytes length:(NSUInteger)length;

初始化对象。 
复制字节数组,设置字节指针指向复制的字节数组,长度为length。

- (nullable instancetype)initWithContentsOfFile:(NSString *)path;

读取文件内容初始化对象。 
读取成功则返回对象,如果失败则返回nil。

- (nullable instancetype)initWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;

读取文件内容初始化对象。 
读取成功则返回对象。如果失败则返回nil,错误信息保存在errorPtr中。 
参数readOptionsMask 指定文件读取选项。

 

typedef NS_OPTIONS(NSUInteger, NSDataReadingOptions) {
    NSDataReadingMappedIfSafe =   1UL << 0, 
    NSDataReadingUncached = 1UL << 1,   
    NSDataReadingMappedAlways  = 1UL << 3,

    NSDataReadingMapped = NSDataReadingMappedIfSafe,
    NSMappedRead = NSDataReadingMapped,
    NSUncachedRead = NSDataReadingUncached
};

- (nullable instancetype)initWithContentsOfURL:(NSURL *)url;

读取url内容初始化对象。
读取成功则返回对象,如果失败则返回nil。

 

- (nullable instancetype)initWithContentsOfURL:(NSURL *)url options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;

读取url内容初始化对象。 
读取成功则返回对象。如果失败则返回nil,错误信息保存在errorPtr中。 
参数readOptionsMask 指定文件读取选项。

- (instancetype)initWithData:(NSData *)data;

根据NSData对象初始化对象。

- (nullable id)initWithContentsOfMappedFile:(NSString *)path

根据文件内容初始化对象。读取文件内容的方式不是read系统调用,而是mmap系统调用。

构造

 

+ (instancetype)data;

构造空的NSData对象。

+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;

根据字节数组构造对象。不复制字节数组。

+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;

根据字节数组构造对象。不复制字节数组。

+ (instancetype)dataWithBytes:(nullable const void *)bytes length:(NSUInteger)length;

根据字节数组构造对象。复制字节数组。

 + (nullable instancetype)dataWithContentsOfFile:(NSString *)path;

根据文件内容构造对象。

+ (nullable instancetype)dataWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;

根据文件内容构造对象。

 + (nullable instancetype)dataWithContentsOfURL:(NSURL *)url;

根据url内容构造对象。

+ (nullable instancetype)dataWithContentsOfURL:(NSURL *)url options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;

根据url内容构造对象。

+ (instancetype)dataWithData:(NSData *)data;

根据NSData对象构造对象。

+ (nullable id)dataWithContentsOfMappedFile:(NSString *)path

根据文件内容构造对象。读取文件内容的方式不是read系统调用,而是mmap系统调用。

返回长度

 

@property (readonly) NSUInteger length;

返回数据

@property (readonly) const void *bytes

返回区间内的数据

- (void)getBytes:(void *)buffer range:(NSRange)range;

参数buffer保存获取的数据,参数range指定获取数据的区间。

- (void)getBytes:(void *)buffer length:(NSUInteger)length;

获取指定长度的数据。如果length大于数据长度,则使用数据长度作为指定长度。

- (void)getBytes:(void *)buffer

获取所有数据。

截取数据

- (NSData *)subdataWithRange:(NSRange)range;

参数range指定截取区间。

是否相等

- (BOOL)isEqualToData:(NSData *)other;

比较数据是否相等。

写入文件

 

- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)writeOptionsMask error:(NSError **)errorPtr;

参数path指定文件路径。参数errorPtr在写入失败时保存出错信息。参数writeOptionsMask 表示写入文件时的可选项,可使用或运算符连接。其可能值为

typedef NS_OPTIONS(NSUInteger, NSDataWritingOptions) {
NSDataWritingAtomic = 1UL << 0, 
NSDataWritingWithoutOverwriting NS_ENUM_AVAILABLE(10_8, 6_0) = 1UL << 1, 

NSDataWritingFileProtectionNone NS_ENUM_AVAILABLE_IOS(4_0)  = 0x10000000,
NSDataWritingFileProtectionComplete NS_ENUM_AVAILABLE_IOS(4_0)  = 0x20000000,
NSDataWritingFileProtectionCompleteUnlessOpen NS_ENUM_AVAILABLE_IOS(5_0)  = 0x30000000,
NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication NS_ENUM_AVAILABLE_IOS(5_0)  = 0x40000000,
NSDataWritingFileProtectionMask NS_ENUM_AVAILABLE_IOS(4_0)  = 0xf0000000,
NSAtomicWrite = NSDataWritingAtomic     
};

NSDataWritingAtomic 表示使用辅助文件完成原子操作。 
NSDataWritingWithoutOverwriting 表示防止覆盖现有文件,不能与NSDataWritingAtomic 结合使用。

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

写入文件。参数path指定文件路径,参数useAuxiliaryFile使用辅助文件完成原子操作。

写入url

- (BOOL)writeToURL:(NSURL *)url options:(NSDataWritingOptions)writeOptionsMask error:(NSError **)errorPtr;

参数path指定url路径。参数errorPtr在写入失败时保存出错信息。参数writeOptionsMask 表示写入时的可选项,可使用或运算符连接。

- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

写入url。参数path指定文件路径,参数atomically完成原子操作。

搜索

- (NSRange)rangeOfData:(NSData *)dataToFind options:(NSDataSearchOptions)mask range:(NSRange)searchRange

搜索数据。参数dataToFind为搜索的数据。参数searchRange为搜索的区间。参数mask 为搜索的方式。搜索方式可使用或运算符连接。 
搜索方式有:

typedef NS_OPTIONS(NSUInteger, NSDataSearchOptions) {
    NSDataSearchBackwards = 1UL << 0,
    NSDataSearchAnchored = 1UL << 1
}

NSDataSearchBackwards表示从后向前搜索。 
NSDataSearchAnchored表示只是搜索头部或尾部(与NSDataSearchBackwards连用)。

与Base64编码相关

- (nullable instancetype)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options
1
解码字符串。options为解码方式。

typedef NS_OPTIONS(NSUInteger, NSDataBase64DecodingOptions) {
    NSDataBase64DecodingIgnoreUnknownCharacters = 1UL << 0
} NS_ENUM_AVAILABLE(10_9, 7_0);

NSDataBase64DecodingIgnoreUnknownCharacters 表示忽略不知道的字符。

- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options
编码为字符串。参数options为编码方式。

typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
    NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
    NSDataBase64Encoding76CharacterLineLength = 1UL << 1,
    NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
    NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,

} NS_ENUM_AVAILABLE(10_9, 7_0);

- (nullable instancetype)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options

解码数据。

- (NSData *)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options
编码数据。

- (nullable id)initWithBase64Encoding:(NSString *)base64String

解码字符串。

- (NSString *)base64Encoding

编码为字符串。

NSMutableData用于保存可变字节数组。
返回数据

@property (readonly) void *mutableBytes

返回长度

@property NSUInteger length;

初始化

- (nullable instancetype)initWithCapacity:(NSUInteger)capacity;

根据容量大小初始化对象。

- (nullable instancetype)initWithLength:(NSUInteger)length;

根据长度初始化对象。数组全部清空为0。

构造

 + (nullable instancetype)dataWithCapacity:(NSUInteger)aNumItems;

根据容量大小构造对象。

 + (nullable instancetype)dataWithLength:(NSUInteger)length;

根据长度构造对象。

添加

- (void)appendBytes:(const void *)bytes length:(NSUInteger)length;

添加数组。

- (void)appendData:(NSData *)other;

添加数据。

替换

- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)bytes;

替换字节数组。

- (void)replaceBytesInRange:(NSRange)range withBytes:(nullable const void *)replacementBytes length:(NSUInteger)replacementLength;

替换字节数组。参数replacementLength指定替换数组的长度。

增加长度

- (void)increaseLengthBy:(NSUInteger)extraLength;

重置

- (void)resetBytesInRange:(NSRange)range;

重置区间内数据为0。

设置

- (void)setData:(NSData *)data; NSData用于保存字节数组。

 

 

 

 

- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)writeOptionsMask error:(NSError **)errorPtr;

参数path指定文件路径。参数errorPtr在写入失败时保存出错信息。参数writeOptionsMask 表示写入文件时的可选项,可使用或运算符连接。其可能值为

typedef NS_OPTIONS(NSUInteger, NSDataWritingOptions) {
NSDataWritingAtomic = 1UL << 0, 
NSDataWritingWithoutOverwriting NS_ENUM_AVAILABLE(10_8, 6_0) = 1UL << 1, 

NSDataWritingFileProtectionNone NS_ENUM_AVAILABLE_IOS(4_0)  = 0x10000000,
NSDataWritingFileProtectionComplete NS_ENUM_AVAILABLE_IOS(4_0)  = 0x20000000,
NSDataWritingFileProtectionCompleteUnlessOpen NS_ENUM_AVAILABLE_IOS(5_0)  = 0x30000000,
NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication NS_ENUM_AVAILABLE_IOS(5_0)  = 0x40000000,
NSDataWritingFileProtectionMask NS_ENUM_AVAILABLE_IOS(4_0)  = 0xf0000000,
NSAtomicWrite = NSDataWritingAtomic     
};

NSDataWritingAtomic 表示使用辅助文件完成原子操作。 
NSDataWritingWithoutOverwriting 表示防止覆盖现有文件,不能与NSDataWritingAtomic 结合使用。

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

写入文件。参数path指定文件路径,参数useAuxiliaryFile使用辅助文件完成原子操作。

写入url

- (BOOL)writeToURL:(NSURL *)url options:(NSDataWritingOptions)writeOptionsMask error:(NSError **)errorPtr;

参数path指定url路径。参数errorPtr在写入失败时保存出错信息。参数writeOptionsMask 表示写入时的可选项,可使用或运算符连接。

- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

写入url。参数path指定文件路径,参数atomically完成原子操作。

搜索

- (NSRange)rangeOfData:(NSData *)dataToFind options:(NSDataSearchOptions)mask range:(NSRange)searchRange

搜索数据。参数dataToFind为搜索的数据。参数searchRange为搜索的区间。参数mask 为搜索的方式。搜索方式可使用或运算符连接。 
搜索方式有:

typedef NS_OPTIONS(NSUInteger, NSDataSearchOptions) {
    NSDataSearchBackwards = 1UL << 0,
    NSDataSearchAnchored = 1UL << 1
}

NSDataSearchBackwards表示从后向前搜索。 
NSDataSearchAnchored表示只是搜索头部或尾部(与NSDataSearchBackwards连用)。

与Base64编码相关

- (nullable instancetype)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options
1
解码字符串。options为解码方式。

typedef NS_OPTIONS(NSUInteger, NSDataBase64DecodingOptions) {
    NSDataBase64DecodingIgnoreUnknownCharacters = 1UL << 0
} NS_ENUM_AVAILABLE(10_9, 7_0);

NSDataBase64DecodingIgnoreUnknownCharacters 表示忽略不知道的字符。

- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options
编码为字符串。参数options为编码方式。

typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
    NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
    NSDataBase64Encoding76CharacterLineLength = 1UL << 1,
    NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
    NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,

} NS_ENUM_AVAILABLE(10_9, 7_0);

- (nullable instancetype)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options

解码数据。

- (NSData *)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options
编码数据。

- (nullable id)initWithBase64Encoding:(NSString *)base64String

解码字符串。

- (NSString *)base64Encoding

编码为字符串。

NSMutableData用于保存可变字节数组。
返回数据

@property (readonly) void *mutableBytes

返回长度

@property NSUInteger length;

初始化

- (nullable instancetype)initWithCapacity:(NSUInteger)capacity;

根据容量大小初始化对象。

- (nullable instancetype)initWithLength:(NSUInteger)length;

根据长度初始化对象。数组全部清空为0。

构造

 + (nullable instancetype)dataWithCapacity:(NSUInteger)aNumItems;

根据容量大小构造对象。

 + (nullable instancetype)dataWithLength:(NSUInteger)length;

根据长度构造对象。

添加

- (void)appendBytes:(const void *)bytes length:(NSUInteger)length;

添加数组。

- (void)appendData:(NSData *)other;

添加数据。

替换

- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)bytes;

替换字节数组。

- (void)replaceBytesInRange:(NSRange)range withBytes:(nullable const void *)replacementBytes length:(NSUInteger)replacementLength;

替换字节数组。参数replacementLength指定替换数组的长度。

增加长度

- (void)increaseLengthBy:(NSUInteger)extraLength;

重置

- (void)resetBytesInRange:(NSRange)range;

重置区间内数据为0。

设置

- (void)setData:(NSData *)data;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MF86

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值