【问题标题】:Swift Array: [AnyObject] vs. [xxxxxxxxClass] and the method "append"Swift Array:[AnyObject] vs. [xxxxxxxxClass] 和方法“append”
【发布时间】:2015-11-24 10:13:39
【问题描述】:

这是我的代码。您无需查看所有内容。我在困惑的地方添加了 cmets:

    class ProductData: NSObject {
        var title = ""
        var icon = ""

        private init(dict: NSDictionary){
            title = dict["title"] as! String
            icon = dict["icon"] as! String
            super.init()
        }

        class func getTheData(fromJSONPath JSONPath: String) -> [ProductData] {
            let JSONData = NSData(contentsOfFile: JSONPath)!
            var JSONArray = [[String : AnyObject]]()
            do {
                JSONArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as! [Dictionary]
            } catch { print("error")}


-----------------------------------------------------------------------------------------    


    //↓↓↓↓↓↓↓↓↓ different: data = "[AnyObject]()" or "[ProductData]()" ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

            var data = [AnyObject]()
    //        var data = [ProductData]()

            for d in JSONArray {
                data.append(ProductData(dict: d))
            }

            return data as! [ProductData]
    //        return data

    //↑↑↑↑↑↑↑↑↑ and here: return "data as! [ProductData]" or "data" ↑↑↑↑↑↑↑↑↑↑↑↑↑↑

        }

    }

我首先使用“var data = [ProductData](), retun data”。没有错误或警告,但是当我运行我的应用程序并运行到代码 data.append(ProductData(dict: d)) 时,它会因错误而崩溃:thread 1:exc_bad_access(code=1,address=0x10)。什么?!

我找到了解决方法:如果我使用var datas = [AnyObject]()return datas as! [ProductData],效果很好。

我很困惑:

  1. 为什么[AnyObject] 使代码正常?
  2. 当我使用[ProductData]时,为什么代码:data.append(ProductData(dict: d))会崩溃?
  3. [AnyObject][ProductData] 有什么区别?

【问题讨论】:

  • 何时/何地崩溃?
  • 您的原始版本适用于我 (screenshot)(仅稍作修改以用于测试我的数据)。你不应该跳这个舞,别的东西惹麻烦了。
  • when:当我创建“ProductData”的实例并想要附加到 Array[ProductData] 时,它会崩溃,但是要附加到 Array[AnyObject] 的相同实例,它运行良好.其中:在此代码中:data.append(ProductData(dict: d))

标签: arrays swift append


【解决方案1】:

您的原始版本适用于我 (screenshot)(仅稍作修改以用于测试我的数据)。你不应该跳这个舞,别的东西惹麻烦了。

我建议稍微清理一下你的课程,并使用guardmaperror 来利用 Swift 2。它将更容易调试,并且无论如何都会更有效地工作。

这是一个例子。唯一的区别是我使用 NSURL 来访问我的案例中的数据并且我已经删除了 icon 值,但是很容易将它改回你的案例。

class ProductData: NSObject {
    var title = ""

    private init(dict: [String : AnyObject]){
        if let t = dict["title"] as? String { self.title = t }
        super.init()
    }

    class func getTheData(fromJSONPath JSONPath: String) -> [ProductData] {

        do {
            // safely unwrap and typecast the values else return empty array
            guard let url = NSURL(string: JSONPath),
                let JSONData = NSData(contentsOfURL: url),
                let JSONArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: [])
                    as? [[String : AnyObject]] else { return [] }

            return JSONArray.map() { ProductData(dict: $0) }

        } catch {
            // this `error` variable is created by the `catch` mechanism
            print(error)
            // return empty array if unkown failure
            return []
        }

    }

}

let test = ProductData.getTheData(fromJSONPath: "http://localhost:5678/file/test.json")

注意:我相信你知道,但为了以防万一,NSData(contentsOf... 是一个同步函数,所以它会阻塞主线程(除非从后台线程执行)。最好尽可能使用异步函数。

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 2020-06-20
    • 2016-11-19
    • 2016-06-30
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2014-11-06
    相关资源
    最近更新 更多