私はiOSアプリケーションにplistファイルを読み込もうと努力してきましたが、これまで使用してきた方法はすべて失敗しました。私は現在http://basememara.com/reading-values-plist-bundle-swift/に記載されている方法を使用していますが、私が試したすべての方法で同じ問題があるようです。変数にplistデータが入らないSwift
これはplistファイルの読み取りコードの場合:
/**
Gets the contents of the specified plist file.
- parameter plistName: property list where defaults are declared
- parameter bundle: bundle where defaults reside
- returns: dictionary of values
*/
public static func contentsOfFile(plistName: String, bundle: Bundle? = nil) -> [String : AnyObject] {
let fileParts = plistName.components(separatedBy: ".")
guard fileParts.count == 2,
let resourcePath = (bundle ?? Bundle.main).path(forResource: fileParts[0], ofType: fileParts[1]),
let contents = NSDictionary(contentsOfFile: resourcePath) as? [String : AnyObject]
let contentsarray = NSArray(contentsOfFile: resourcePath)
else { return [:] }
return contents
}
をし、通話が
変数の内容とContentsArray(ように見えるのplistで
let values = Bundle.contentsOfFile(plistName: "Event.plist")
print(values["Item0"]) // My string value 1.
ですテスト用)には何もありません。私はresourcePathが正しいことを確認しました。エラーはありませんので、何が間違っているのか分かりません。他のすべてのメソッドには正しいパスがありますが、コンテンツ変数は決して満たされません。
ファイルが対象メンバーシップであるかどうかがチェックされ、ファインダ内に存在していると見なされますが、どちらのメソッドもnilを出力します。 – traisjames
ファイルの場所は何ですか?メインバンドルに入っているのですか、キャッシュのような他のディレクトリに保存していてどこかからダウンロードしていますか? – Avi
メインバンドル(.../6922A277-91D7-4F70-A03F-0DD6541C89EF/LG%20Sim%20Builder.app/Event.plist)にあります – traisjames