小编Che*_*yx 的帖子

Apple Swift文件IO操作

我正在搜索Swift文件输入/输出操作,以便下周在我的大学进行简短的演示.我真的没有太多关于它.

以下是我发现的操作:

let location = "/Users/test/test.txt"
var str = "Hello, playground"

//write
str.writeToFile( location, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
//read
let filecontent = String(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)
//binary read
let test = NSData(contentsOfFile: location)
//if file exists
NSFileManager().fileExistsAtPath(location)
//find Files in the App Bundle with suffix test and prefix txt
var path= NSBundle.mainBundle().pathForResource("test", ofType: "txt")
//path for dynamic user names
if let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as?  [String] {
let path = dirs[0].stringByAppendingPathComponent( "file.txt")
//printout: /Users/test/Documents/file.txt …
Run Code Online (Sandbox Code Playgroud)

file-io file swift

6
推荐指数
1
解决办法
3283
查看次数

标签 统计

file ×1

file-io ×1

swift ×1