gpt4 book ai didi

ios - Swift Plist 读取和初始化数组作为键的对象

转载 作者:IT王子 更新时间:2023-10-29 05:23:26 25 4
gpt4 key购买 nike

我正在用 Swift 开发一个基于 UITableView 的小型应用程序。我开始对一些基本数组进行硬编码(包括基本类声明和 UITableView socket 声明:

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"]

此代码位于 UITableViewController 中,它有一个连接到 UITableView 的 socket 。我为此创建了一个名为“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"];

我开始在 Swift 中这样做是这样的:

let path = NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist")

let dict = NSDictionary(contentsOfFile:path) // Error: 'ScenesViewController.Type' does not have a member named 'path'

我在 Internet 上搜索了解决方案,但找不到。所以我采取了下一步行动 - “几乎”单线:

let dict = NSDictionary(contentsOfFile: 
NSBundle.mainBundle().pathForResource("Scenes", ofType:"plist"))

var sceneName = dict["SceneName"] // Error: 'ScenesViewController.Type' does not have a member named 'dict'

现在,我几乎可以求助的唯一解决方案是将每个数组的所有内容都放在一行中:

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! = ...)

所以我添加了显式类型注释只是为了发现还有更多的错误。在 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
}

实际上有两个错误;我不知道这是否是不小心重复的。下一个错误在 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
}
}

同样,有两个错误;但是,我相信这是因为 sceneDetailsceneName 都被使用了。在 Objective-C 中不存在(或者我没有遇到过)的文件中读取和使用 plist 似乎到处都是错误。

最佳答案

要解决您最初的问题,请将代码放入函数中,例如:

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

关于ios - Swift Plist 读取和初始化数组作为键的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24209556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com