5

How can I write array of Floatto binary file and then read it?

var array: [Float]: [0.1, 0.2, 0.3]

func writeArrayToBinary(array: [Float]) {
    //...
}

func readArrayFromBinary() -> [Float] {
    //...
}
6
  • 1
    before you are going design you own binary protocol check catb.org/esr/writings/taoup/html/ch05s01.html Commented Nov 19, 2015 at 20:38
  • does it have to be binary, or can it be a plist? Commented Nov 19, 2015 at 20:38
  • Well I would suggest you to write it to a plist (array as NSArray).writeToFile(yourPath, atomically: false) Commented Nov 19, 2015 at 20:45
  • @Knight0fDragon, my priority is i/o speed. plist is suitable for this.. as I understand? Commented Nov 19, 2015 at 20:51
  • 1
    Sure... Wait I will write you how to do it... Commented Nov 19, 2015 at 20:57

2 Answers 2

16

As you stated in a comment, speed is priority. Then, I suggest you write your array to a binary file (as originally requested), using the Data class, provided with Cocoa.

let url = URL(fileURLWithPath: "myTestFile.myBinExt")

// Writing
var wArray: [Float] = [1.1, 3.7, 2.5, 6.4, 7.8]
let wData = Data(bytes: &wArray, count: wArray.count * MemoryLayout<Float>.stride)
try! wData.write(to: url)

// Reading file
let rData = try! Data(contentsOf: url)

// Converting data, version 1
var rArray: [Float]?

rData.withUnsafeBytes { (bytes: UnsafePointer<Float>) in
    rArray = Array(UnsafeBufferPointer(start: bytes, count: rData.count / MemoryLayout<Float>.size))
}

print(rArray!)

// Converting data, version 2
let tPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: rData.count)
rData.copyBytes(to: tPointer, count: rData.count)

defer {
    tPointer.deinitialize(count: rData.count)
    tPointer.deallocate(capacity: rData.count)
}

var pointer = UnsafeRawPointer(tPointer) // Performs no allocation or copying; no deallocation shall be done.

// MemoryLayout<Float>.size = 4
print(pointer.load(fromByteOffset: 00, as: Float.self))
print(pointer.load(fromByteOffset: 04, as: Float.self))
print(pointer.load(fromByteOffset: 08, as: Float.self))
print(pointer.load(fromByteOffset: 12, as: Float.self))
print(pointer.load(fromByteOffset: 16, as: Float.self))

Output:

[1.10000002, 3.70000005, 2.5, 6.4000001, 7.80000019]
1.1
3.7
2.5
6.4
7.8
Sign up to request clarification or add additional context in comments.

Comments

5

Please try this...

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.