私は助けなしにこれを解決することを望んでいましたが、難しい問題ではないことは分かっていますが、サークルに入っているようです。だから、どんな助けでも大歓迎です。SwiftのダウンロードしたplistからTableViewにデータを入力
plistのデータからマップとTableViewを読み込むようにアプリケーションをセットアップしました。すべてうまくいく。しかし今、私はもちろん、ドキュメントディレクトリにファイルを配置するサーバーからplistをダウンロードしています。私の質問は、バンドルからではなくディレクトリからこれを読み取るSwiftコードは何ですか?
これは、Appバンドルから読み込むコードの一部です(すべて動作しますが、ここではすべてのコードを提供していません)。Apple Developerサイトを参照してください -
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")
}
}
}