NSOutputStream用Swift编写&读一个Double()

时间:2015-05-18 15:59:44

标签: xcode swift

//将Double写入文件,工作正常

func writeData(){

var oStream: NSOutputStream?
let folder = NSSearchPathForDirectoriesInDomains(.DesktopDirectory, .UserDomainMask, true)[0] as! String
let path = folder.stringByAppendingPathComponent(fileName)
oStream = NSOutputStream(toFileAtPath: path, append: false)
oStream!.open()

var val = 9.81
let foo = NSData(bytes: &val, length: sizeof(Double))
oStream!.write(UnsafePointer(foo.bytes), maxLength: sizeof(Double))
oStream!.close()

}

//从文件中读取双重

func readData(){

var iStream: NSInputStream?
let folder = NSSearchPathForDirectoriesInDomains(.DesktopDirectory, .UserDomainMask, true)[0] as! String
let path = folder.stringByAppendingPathComponent(fileName)
iStream = NSInputStream(fileAtPath: path)
iStream!.open()

var buffer = [UInt8](count: sizeof(Double), repeatedValue: 0)
iStream!.read(&buffer, maxLength: sizeof(Double))

// Question: how to get the Double value back out of buffer?

iStream!.close()

}

0 个答案:

没有答案