在swift 3游乐场阅读plist

时间:2023-01-23 22:19:42

I have followed loads of questions here but nothing seems to work.

我已经在这里跟踪了大量的问题,但似乎没有任何效果。

I am using Swift3 in a Playground. Running on El Capitan and Xcode 8.1. I have a plist with the root as a Dictionary containing one Int value and two 2D Arrays of Ints.

我在操场上玩Swift3。在El Capitan和Xcode 8.1上运行。我有一个plist,它的根作为一个字典,包含一个Int值和两个二维Int数组。

plist

plist

Every question I follow does not seem to work the closest I have got is for the playground to not return errors but it seems to be constantly running (the spinning icon never stops).

我所关注的每一个问题似乎都不能让我得到的最接近的答案是操场不返回错误,但它似乎一直在运行(旋转图标永不停止)。

my current code, I believe to be the closest I have achieved.

我现在的代码,我认为是我最近完成的。

import Foundation 
if let path = Bundle.main.path(forResource: "levelList", ofType: "plist") {
    let plistXML = FileManager.default.contents(atPath: path)!

    let mydata = try! PropertyListSerialization.propertyList(from: plistXML, options: [], format: nil) as! [String:Any]
}

other options I have tried from previous stack overflow answers in a similar context.

我在类似的上下文中尝试过的其他选项。

let mydata = Dictionary(fromPropertyList: path, format: "XML") as! [String: Any]
******
let mydata = Dictionary(contentsOf: path) as? [String: Any]

The data was added to the resources folder correctly as the linked question gave instructions for. I have restarted Xcode(and mac) as suggested in the comments. After a while the execution stopped with error "error: Execution was interrupted reason exc_bad_access (code=1 address=0x0)" After another restart the code works. How do would i extract the data into an array in swift since at the moment the playground is showing ["Level 2":["Col": <_NS_array0 0x7fc620d091a0>(

数据被正确地添加到resources文件夹中,因为链接的问题给出了说明。在评论中,我已经重新启动了Xcode(和mac)。过了一会儿,执行停止了,出现了错误“error:执行被中断,原因是exc_bad_access(代码=1地址=0x0)”再重新启动之后,代码就可以工作了。我如何用swift将数据提取到一个数组中,因为此时游乐场正在显示["Level 2":["Col": <_NS_array0 0x7fc620d091a0>(

2 个解决方案

#1


1  

You are using the wrong API, you need to load Data rather than something in the file system.

您正在使用错误的API,您需要加载数据而不是文件系统中的某些东西。

if let url = Bundle.main.url(forResource: "levelList", withExtension: "plist"),
   let plistData = try? Data(contentsOf: url ) {
      let mydata = try! PropertyListSerialization.propertyList(from: plistData, options: [], format: nil) as! [String:Any]
}

#2


0  

To finally get the program working I used.

为了让我用过的程序运行起来。

devices = NSArray(contentsOfFile: path) as! [AnyObject]

The issue with playground continuously running was solved by going to activity monitor and force quitting the process named com.apple.coresimulator which was identified as not responding. After doing this the playground ran instantly.

操场持续跑步的问题通过活动监控和强迫退出这个名为com.apple的程序得到了解决。coresimulator被识别为不响应。这样做之后,操场立刻跑起来。

#1


1  

You are using the wrong API, you need to load Data rather than something in the file system.

您正在使用错误的API,您需要加载数据而不是文件系统中的某些东西。

if let url = Bundle.main.url(forResource: "levelList", withExtension: "plist"),
   let plistData = try? Data(contentsOf: url ) {
      let mydata = try! PropertyListSerialization.propertyList(from: plistData, options: [], format: nil) as! [String:Any]
}

#2


0  

To finally get the program working I used.

为了让我用过的程序运行起来。

devices = NSArray(contentsOfFile: path) as! [AnyObject]

The issue with playground continuously running was solved by going to activity monitor and force quitting the process named com.apple.coresimulator which was identified as not responding. After doing this the playground ran instantly.

操场持续跑步的问题通过活动监控和强迫退出这个名为com.apple的程序得到了解决。coresimulator被识别为不响应。这样做之后,操场立刻跑起来。