写浮点数组到二进制文件,并迅速读取它
我如何写Float
到二进制文件,然后阅读它?
var array: [Float]: [0.1, 0.2, 0.3] func writeArrayToBinary(array: [Float]) { //... } func readArrayFromBinary() -> [Float] { //... }
请试试这个…
var array: [Float] = [0.1, 0.2, 0.3] func writeArrayToPlist(array: [Float]) { if let arrayPath: String = createArrayPath() { (array as NSArray).writeToFile(arrayPath, atomically: false) } } func readArrayFromPlist() -> [Float]? { if let arrayPath: String = createArrayPath() { if let arrayFromFile: [Float] = NSArray(contentsOfFile: arrayPath) as? [Float] { return arrayFromFile } } return nil } func createArrayPath () -> String? { if let docsPath: String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last { return ((docsPath as NSString).stringByAppendingPathComponent("myArrayFileName") as NSString).stringByAppendingPathExtension("plist") } return nil }