arrays - Swift - NSDictionary 多维

标签 arrays swift class structure nsdictionary

目前我正在研究一个初始化多维数组的类:

class Manager {

    var data: Dictionary<String,NSDictionary>

    init(data: Dictionary<String,NSDictionary>) {

        func getApiData() {

            getApiDataResource() { responseObject, error in

                let resourceA = responseObject!

                self.data["resources"] = resourceA

            }

        }

    }  

}

异步返回的 responseObject 具有以下结构:

{
    "data": [
        {
            id: 1
            name: "Name1"
        },
        {
            id: 2
            name: "Name2"
        }
    ],
    "count": 2,
    "success": true
}

我想在我的“数据”变量中获取的结构:

{
    "resources": [
        {
            "resourceA":
                {
                    "data": [
                        {
                            id: 1
                            name: "Name1"
                        },
                        {
                            id: 2
                            name: "Name2"
                        }
                    ],
                    "count": 2,
                    "success": true
                }
        },
        {
            "resourceB": // and so on ...
        }
    ]
}

但是当将我的 responseObject 保存到我的“数据”变量中时:

self.data["resources"] = resourceAData // this line fails

它打印:

Cannot assign to the result of this expression

谁能帮我解决这个问题??

谢谢和问候!

最佳答案

我假设有一个定义如下的函数:

func getApiDataResource(callback: (responseObject:NSDictionary?, error:NSError?) -> ()) {
    // data retrieved from server at warp 8 here and other futuristic things
}

第 1 步(必填)

我将函数 getApiData 移到了 init 之外。

class Manager {
    var data: Dictionary<String,NSDictionary>
    init(data: Dictionary<String,NSDictionary>) {
        getApiData()
    }
    func getApiData() {
        getApiDataResource() { responseObject, error in
            let resourceA = responseObject!
            self.data["resources"] = resourceA
        }
    }
}

现在编译器提示这个:

Use of self in method call getApiData before all store properties are initialized

还有这个

Return from initializer without initializing all stored properties (As stated by you in a comment) Fine. Let's fix it.

第 2 步(必填)

class Manager {

    var data: Dictionary<String,NSDictionary>

    init(data: Dictionary<String,NSDictionary>) {
        self.data = data
        getApiData()
    }

    func getApiData() {
        getApiDataResource() { responseObject, error in
            let resourceA = responseObject!
            self.data["resources"] = resourceA
        }
    }
}

现在它可以正确编译。

但是我会调整一些其他的东西。

第 3 步(建议)

在尾随闭包中,您使用的是 !。您应该尽可能避免这种情况。而且您没有检查可能不为零的 error 参数。

您应该使用条件解包,这是强制解包的安全替代方案(您使用!)。

class Manager {

    var data: Dictionary<String,NSDictionary>

    init(data: Dictionary<String,NSDictionary>) {
        self.data = data
        getApiData()
    }

    func getApiData() {
        getApiDataResource() { responseObject, error in
            if let resourceA = responseObject where error == nil {
                self.data["resources"] = resourceA
            }
        }
    }
}

现在,如果闭包中的 responseObjectnil,代码不会崩溃。

第 4 步(只是一个偏好)

这只是一种风格偏好。 Swift 允许您以这种方式定义字典(就像您所做的那样)。

Dictionary<String,NSDictionary>

或者这样:

[String:NSDictionary]

2个语句是等价的。我只是觉得第二个更重要。所以……

class Manager {

    var data: [String:NSDictionary]

    init(data: [String:NSDictionary]) {
        self.data = data
        getApiData()
    }

    private func getApiData() {
        getApiDataResource() { responseObject, error in
            if let resourceA = responseObject where error == nil {
                self.data["resources"] = resourceA
            }
        }
    }
}

让我知道这就是您所需要的!

关于arrays - Swift - NSDictionary 多维,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732646/

相关文章:

python - 通过对数组中的 n 个连续数字进行平均来构建数组

java - 有什么办法可以修复这个数组越界异常吗?

arrays - 无法下标类型 '[UInt32]' 的值

arrays - 在循环遍历 awk 时删除数组元素 : always safe?

javascript - TypeScript接口(interface)签名 "(): string"

ios - iPhone 11 和/或 iOS 13 的 iBeacon 监控/测距问题

ios - Swift 嵌入式库 -> Command/usr/bin/codesign 失败,退出代码为 1

swift - 如何在 swift 中声明不同类型的二维数组?

c++ - 继承自c++标准库List类,增加更多方法

javascript - 类应该替换 javascript 中的构造函数吗?