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

前端之家收集整理的这篇文章主要介绍了ios – Swift Plist读取和初始化数组作为键的对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在 Swift开发一个小型的基于UITableView的应用程序.我开始硬编码一些基本的数组(包括基本的类声明和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"]

这个代码在一个UITableViewController里面,它有一个插座,它连接到一个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"];

我开始在Swift这样做:

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

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

我扫过互联网找到一个解决方案,但找不到一个.所以我采取了下一个行动方案 – “几乎”一线:

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里面:function:

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

再次,有两个错误;但是,我相信这是因为sceneDetail和sceneName都被使用.在Objective-C中阅读和使用不存在的文件(或者我没有遇到的文件)中,似乎有错误.

提前致谢,

-KodInc

解决方法

解决你的初始问题,把代码放在一个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>
}
原文链接:https://www.f2er.com/iOS/336203.html

猜你在找的iOS相关文章