Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Swift-nsd_Arrays_Swift_Class_Structure_Nsdictionary - Fatal编程技术网

Arrays Swift-nsd

Arrays Swift-nsd,arrays,swift,class,structure,nsdictionary,Arrays,Swift,Class,Structure,Nsdictionary,目前我正在处理一个初始化多维数组的类: class Manager { var data: Dictionary<String,NSDictionary> init(data: Dictionary<String,NSDictionary>) { func getApiData() { getApiDataResource() { responseObject, error in

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

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

            }

        }

    }  

}
我希望在“数据”变量中获得的结构:

{
    "resources": [
        {
            "resourceA":
                {
                    "data": [
                        {
                            id: 1
                            name: "Name1"
                        },
                        {
                            id: 2
                            name: "Name2"
                        }
                    ],
                    "count": 2,
                    "success": true
                }
        },
        {
            "resourceB": // and so on ...
        }
    ]
}
self.data["resources"] = resourceAData // this line fails
但在将responseObject保存到“数据”变量中时:

{
    "resources": [
        {
            "resourceA":
                {
                    "data": [
                        {
                            id: 1
                            name: "Name1"
                        },
                        {
                            id: 2
                            name: "Name2"
                        }
                    ],
                    "count": 2,
                    "success": true
                }
        },
        {
            "resourceB": // and so on ...
        }
    ]
}
self.data["resources"] = resourceAData // this line fails
它打印:

Cannot assign to the result of this expression
谁能帮我解决这个问题


谢谢和问候

您有
let data:Dictionary
,应该是
var data:Dictionary
我假设有一个函数定义如下:

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
        }
    }
}
现在它确实可以正确编译

不过,我会调整其他一些事情

步骤3(建议) 在尾部闭包中,您正在使用
。如果可能的话,你应该避免这样做。您没有检查可能不是零的
error
参数

您应该使用
条件展开
,这是
强制展开
的安全替代方法(您使用了

或者这样:

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

让我知道这是你需要的

您需要分配给resourceA,而不是resources。更正了它,但这不是错误:/n您正在
init
中声明函数
getApiData
。有什么特别的原因吗?谢谢你的回答:但它会打印:“无法将'NSDictionary'类型的值分配给'Array'类型的值”现在你有一个强制转换问题,意思是你正在传递一个NSDictionary,并期望它进入Array我真的不确定,现在处理它很热,你能帮我吗?
var-data:Dictionary
可能就是你想要的,为什么它必须是Arraythanks,但是现在变量“self.data”甚至在初始化之前就被使用了,这是因为数据是异步退役的吗?
[String:NSDictionary]
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
            }
        }
    }
}