Here是我的档案。
我该如何编写这个数组:
.txt
到file.txt
个文件(function run(n) {
return Math.pow(16, -n) * (4 / (8 * n + 1) - 2 / (8 * n + 4) - 1 / (8 * n + 5) - 1 / (8 * n + 6));
}
function convertFromBaseToBase(str, fromBase, toBase) {
var num = parseInt(str, fromBase);
return num.toString(toBase);
}
for (var i = 0; i < 10; i++) {
var a = run(i);
console.log(convertFromBaseToBase(a, 16, 10));
}
)?我知道还有其他问题,但他们用其他语言(不是Swift)。我希望它可以在真正的iPhone上运行。如有必要,我很乐意提供更多信息。我是Swift的新手,请原谅我的问题,如果它看起来太简单了。 注意:我具体询问有关包含数组的数组。
提前致谢!
答案 0 :(得分:6)
answered you中已your other question:
这是一个完整的Playground示例:
let fileUrl = NSURL(fileURLWithPath: "/tmp/foo.plist") // Your path here
let listOfTasks = [["Hi", "Hello", "12:00"], ["Hey there", "What's up?", "3:17"]]
// Save to file
(listOfTasks as NSArray).writeToURL(fileUrl, atomically: true)
// Read from file
let savedArray = NSArray(contentsOfURL: fileUrl) as! [[String]]
print(savedArray)
答案 1 :(得分:2)
您应该使用NSKeyedArchiver / NSKeyedUnarchiver将数组读/写为属性列表文件而不是文本文件。您可以将文件保存到Library文件夹中的首选项文件夹:
let preferencesDirectory = try! NSFileManager().URLForDirectory(.LibraryDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent("Preferences", isDirectory: true)
let listOfTasksURL = preferencesDirectory.URLByAppendingPathComponent("listOfTasks.plist")
var listOfTasks: [[String]] {
get {
return NSKeyedUnarchiver.unarchiveObjectWithFile(listOfTasksURL.path!) as? [[String]] ?? []
}
set {
NSKeyedArchiver.archiveRootObject(newValue, toFile: listOfTasksURL.path!)
}
}
如果您想在游乐场文件中测试它,您需要将其保存到文档目录:
let documentsDirectory = try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let listOfTasksURL = documentsDirectory.URLByAppendingPathComponent("listOfTasks.plist")
var listOfTasks: [[String]] {
get {
return NSKeyedUnarchiver.unarchiveObjectWithFile(listOfTasksURL.path!) as? [[String]] ?? []
}
set {
NSKeyedArchiver.archiveRootObject(newValue, toFile: listOfTasksURL.path!)
}
}
listOfTasks = [["Hi", "Hello", "12:00"],["Hey there", "What's up?", "3:17"]]
listOfTasks // [["Hi", "Hello", "12:00"], ["Hey there", "What's up?", "3:17"]]