Swift对文件的简单读写操作

前端之家收集整理的这篇文章主要介绍了Swift对文件的简单读写操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
let file = "file.txt" 
if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask,true) as? [String] { 
    let dir = dirs[0] //documents directory 
    let path = dir.stringByAppendingPathComponent(file); 
    let text = "some text" //writing
    text.writeToFile(path,atomically: false,encoding: NSUTF8StringEncoding,error: nil); //reading 
    let text2 = String(contentsOfFile: path,error: nil) 
}

猜你在找的Swift相关文章