
import UIKitclass ScenesVIEwController: UItableVIEwController {@IBOutlet var mytableVIEw: UItableVIEwvar 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>} 总结 以上是内存溢出为你收集整理的ios – Swift Plist读取和初始化数组作为键的对象全部内容,希望文章能够帮你解决ios – Swift Plist读取和初始化数组作为键的对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)