Kod*_*Inc 13 nsdictionary plist uitableview ios swift
我正在UITableView
Swift 开发一个基于小程序的应用程序.我开始硬编码一些基本数组(包括基本类声明和UITableView
插座声明:
import UIKit
class ScenesViewController: UITableViewController {
@IBOutlet var myTableView: UITableView
var sceneName = ["Scene 1", "Scene 2", "Scene 3", "Scene 4", "Scene 5"]
var sceneDetail = ["Hi", "Hello", "Bye", "My Bad", "Happy"]
Run Code Online (Sandbox Code Playgroud)
这个代码在一个UITableViewController
有一个插座的内部,所有插座都连接到a UITableView
.我为此创建了一个名为"Scenes.plist"的plist,并试图替换代码.在Objective-C中,我会这样做:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Scenes" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
sceneName = [dict objectForKey:@"SceneName"];
sceneDetail = [dict objectForKey:@"SceneDetail"];
Run Code Online (Sandbox Code Playgroud)
我开始在Swift中这样做:
let path = NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist")
let dict = NSDictionary(contentsOfFile:path) // Error: 'ScenesViewController.Type' does not have a member named 'path'
Run Code Online (Sandbox Code Playgroud)
我在互联网上搜索解决方案,但无法找到解决方案.所以我采取了下一步行动 - 一个'几乎'单行:
let dict = NSDictionary(contentsOfFile:
NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist"))
var sceneName = dict["SceneName"] // Error: 'ScenesViewController.Type' does not have a member named 'dict'
Run Code Online (Sandbox Code Playgroud)
现在,我几乎可以采用的唯一解决方案是在每个阵列的一行中拥有所有这些:
var sceneName = NSDictionary(contentsOfFile:
NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist").objectForKey("SceneName")
// Warning: Variable 'sceneName' inferred to have type 'AnyObject!', which may be unexpected
// Fix-It: Add an explicit type annotation to silence this warning (var sceneName: AnyObject! = ...)
Run Code Online (Sandbox Code Playgroud)
所以我添加了显式类型注释,以发现存在更多错误.cellForRowAtIndexPath:
功能内部:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell: UITableViewCell = tableView!.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = sceneName[indexPath!.row] // Error: could not find member 'row'
return cell
}
Run Code Online (Sandbox Code Playgroud)
实际上有两个错误 ; 我不知道这是否意外重复.下一个错误在prepareForSegue:sender:
方法中:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue?.identifier == "ShowDetails" {
var detailVC: ScenesDetailViewController = segue!.destinationViewController as ScenesDetailViewController
var myIndexPath: NSIndexPath = myTableView.indexPathForSelectedRow()
var row: Int = myIndexPath.row
detailVC.detailModal = [sceneDetail[row], sceneName[row]] // Error: Could not find an overload for 'subscript' that accepts the supplied arguments
}
}
Run Code Online (Sandbox Code Playgroud)
同样,有两个错误; 但是,我相信这是因为sceneDetail
并且sceneName
都被使用了.在Objective-C中,在那些不存在(或者我没有遇到过)的文件中读取和使用plist时,似乎总是出现错误.
提前致谢,
-KodInc
pai*_*aiv 19
要解决您的初始问题,请将代码放在func中,例如:
var memoryName = []
var memoryDetail = []
override func awakeFromNib() {
super.awakeFromNib()
let path = NSBundle.mainBundle().pathForResource("Memories", ofType:"plist")
let dict = NSDictionary(contentsOfFile:path)
memoryName = dict["MemoryName"] as Array<String>
memoryDetail = dict["MemoryDetail"] as Array<String>
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16228 次 |
最近记录: |