0
我一直希望能在没有帮助的情况下解决这个问题,但即使我知道这不是一个困难的问题,但我似乎仍然在圈子里。所以,任何帮助非常感谢。从Swift下载的plist中填充TableView
我已经建立了我的应用程序,用plist中的数据填充地图和TableView。一切运作良好。但是现在我从一台服务器下载plist,当然,这个服务器将文件放在一个文档目录中。我的问题是,什么是从目录而不是从该目录中读取这个Swift代码?
这是从应用程序包中读取的一些代码(请注意,这一切都可行,但我并未在此提供所有代码,请不要指向Apple开发人员站点 - 我已查找过):
if let path = NSBundle.mainBundle().pathForResource("testData", ofType: "plist"){
if let arrayOfDictionaries = NSArray(contentsOfFile: path){
for dict in arrayOfDictionaries {
tableData.append(dict.objectForKey("title") as! String)
stateData.append(dict.objectForKey("state") as! String)
codeData.append(dict.objectForKey("code") as! String)
infoData.append(dict.objectForKey("info") as! String)
}
}
这是从服务器下载的plist代码:好不容易才解决这
func URLSession(session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didFinishDownloadingToURL location: NSURL){
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = NSFileManager()
let destinationURLForFile = NSURL(fileURLWithPath: documentDirectoryPath.stringByAppendingString("/testData.plist"))
if fileManager.fileExistsAtPath(destinationURLForFile.path!){
showFileWithPath(destinationURLForFile.path!)
}
else{
do {
try fileManager.moveItemAtURL(location, toURL: destinationURLForFile)
// show file
showFileWithPath(destinationURLForFile.path!)
}catch{
print("An error occurred while moving file to destination url")
}
}
}