创建 NSData 对象

使用文件

迅速

let data = NSData(contentsOfFile: filePath) //assuming filePath is a valid path

Objective-C

NSData *data = [NSData dataWithContentsOfFile:filePath]; //assuming filePath is a valid path

使用 String 对象

迅速

let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding) //assuming string is a String object

Objective-C

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; //assuming string is a String object