我正在尝试使用 plist
来保存字典数组。一切似乎都很好,除了当我尝试遍历数组的每个元素时。在本例中,我使用了 NSArray
和 NSDictionary
。
在下面的例子中,albumInfo
是一个 NSDictionary
。键 "Title"
与字符串对象链接。我正在使用一个名为 addAlbumInfo
的方法,它的参数都是 String
,除了 "price:"
请帮忙。
var pathToAlbumsPlist:NSString = NSBundle.mainBundle().pathForResource("AlbumArray", ofType: "plist");
var defaultAlbumPlist:NSArray = NSArray(contentsOfFile: pathToAlbumsPlist);
for albumInfo:NSDictionary in defaultAlbumPlist {
self.addAlbumWithTitle(
albumInfo["title"], //Says it is not convertable to a string
artist: albumInfo["artist"],
summary: albumInfo["summary"],
price: albumInfo["price"].floatValue,
locationInStore: albumInfo["locationInStore"]
);
}
最佳答案
由于 albumInfo 是一个 NSDictionary,它只能包含 AnyObjects,这意味着它不能包含作为结构的 Swift 字符串。如果您的 addAlbumWithTitle 需要一个 String 并且您尝试将它传递给 AnyObject ,它将给您这个错误。您可以像这样将 albumInfo["title"]
转换为 Swift 字符串:
albumInfo["title"] as String
关于ios - Swift:遍历 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24727308/