API参考Foundation

NSData

二进制数据对象,用于读写文件、字符串编码、网络等。

用于表示二进制数据。常用创建方式:从文件/URL 读取、从字符串编码、从字节。编码常用值:4 表示 UTF-8(NSUTF8StringEncoding)。

类成员 (Class members)

data

返回空数据。

static data(): NSData

Return Value:

  • NSData: 空数据实例。

dataWithStringEncoding

从字符串与编码创建。

static dataWithStringEncoding(string: string, encoding: number): NSData

Parameters:

NameTypeDescription
stringstring源字符串。
encodingnumber编码(4 为 UTF-8)。

Return Value:

  • NSData: 新数据。

dataWithContentsOfFile

从文件路径读取。

static dataWithContentsOfFile(path: string): NSData

Parameters:

NameTypeDescription
pathstring文件路径。

Return Value:

  • NSData: 新数据。

dataWithContentsOfURL

从 URL 读取。

static dataWithContentsOfURL(url: NSURL): NSData

Parameters:

NameTypeDescription
urlNSURL资源 URL。

Return Value:

  • NSData: 新数据。

dataWithBytesLength

从字节与长度创建。

static dataWithBytesLength(bytes: any, length: number): NSData

Parameters:

NameTypeDescription
bytesany字节序列。
lengthnumber长度。

Return Value:

  • NSData: 新数据。

dataWithData

拷贝数据。

static dataWithData(data: NSData): NSData

Parameters:

NameTypeDescription
dataNSData源数据。

Return Value:

  • NSData: 新数据。

实例成员 (Instance members)

属性

属性类型说明
lengthnumber字节数。
bytes只读原始字节。
base64EncodingstringBase64 编码字符串。

writeToFileAtomically

写入文件。

writeToFileAtomically(path: string, useAuxiliaryFile: boolean): boolean

Parameters:

NameTypeDescription
pathstring文件路径。
useAuxiliaryFileboolean是否先写临时再替换。

Return Value:

  • boolean: 是否成功。

writeToURLAtomically

写入 URL。

writeToURLAtomically(url: NSURL, atomically: boolean): boolean

Parameters:

NameTypeDescription
urlNSURL目标 URL。
atomicallyboolean是否原子写入。

Return Value:

  • boolean: 是否成功。

subdataWithRange

取子数据。

subdataWithRange(range: any): NSData

Parameters:

NameTypeDescription
rangeany范围(如 { location, length })。

Return Value:

  • NSData: 子数据。

相关

头文件API清单

正在加载...

协议:JSBNSData,来源:-