2017-07-31 3 views
0

私は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 
} 

をし、通話が

enter image description here

変数の内容とContentsArray(ように見えるのplistで

let values = Bundle.contentsOfFile(plistName: "Event.plist") 
print(values["Item0"]) // My string value 1. 

ですテスト用)には何もありません。私はresourcePathが正しいことを確認しました。エラーはありませんので、何が間違っているのか分かりません。他のすべてのメソッドには正しいパスがありますが、コンテンツ変数は決して満たされません。

答えて

0
if let path = Bundle.main.path(forResource: "Test", ofType: "plist") { 
     let myDict = NSDictionary(contentsOfFile: path) 
     print(myDict) 
} 

もう一つの方法 -

if let url = Bundle.main.url(forResource:"Test", withExtension: "plist"), 
     let myDict = NSDictionary(contentsOf: url) as? [String:Any] { 
     print(myDict) 
} 

また、それは同様にターゲットのメンバーシップをターゲットに何かがあるかもしれません。プロジェクトナビゲータでplistファイルを選択し、File InpectorのTarget Membershipセクションをチェックします。

+0

ファイルが対象メンバーシップであるかどうかがチェックされ、ファインダ内に存在していると見なされますが、どちらのメソッドもnilを出力します。 – traisjames

+0

ファイルの場所は何ですか?メインバンドルに入っているのですか、キャッシュのような他のディレクトリに保存していてどこかからダウンロードしていますか? – Avi

+0

メインバンドル(.../6922A277-91D7-4F70-A03F-0DD6541C89EF/LG%20Sim%20Builder.app/Event.plist)にあります – traisjames

関連する問題