这里有些困境。我的方法GetData()将数组存储在jsondata1中。我将如何在另一个类中使用该数组?我已经将其设置为Data的子类。谢谢!
class Data {
let parameters = ["test": ViewController().myinfo]
func GetData() {
AF.request("my url", parameters: parameters).responseDecodable(of: Array<JsonData>.self ) { (response) in
let jsondata1 = response.value
// debugPrint(response)
print(jsondata1)
}
}
}
class Testing : Data {
func AnnualTesting() {
//How would I reference jsondata1 here?
debugPrint(jsondata1)
}
}
答案 0 :(得分:-1)
您对jsondata1的声明在某个块的范围内。 从外面看不到。
您必须在类的接口中声明它, 就像您对变量“参数”所做的一样。
通常,如果在接口中声明了变量,则可以从另一个类访问变量。可以将其声明为实例变量或静态(类)变量。而且,您应该具有用于访问实例var的类的实例,并且不应具有用于访问class / static变量的实例。
或者像您一样,通过继承。
答案 1 :(得分:-1)
几件事...看评论。
class Data {
// This kind of thing is the source of more bugs than I can count.
// Are you *sure* you want to create a brand new ViewController that's connected to nothing?
let parameters = ["test": ViewController().myinfo]
// Declare an instance variable so it's visible to the subclass
// (Not sure what data type you want to use here. I'm calling it a String for convenience.)
var jsondata1: String?
func GetData() {
AF.request("my url", parameters: parameters).responseDecodable(of: Array<JsonData>.self ) { (response) in
// Note that this is filled in asynchronously,
// so isn't available as soon as the function is called.
// Currently you have no way of knowing in other code when the result *is* available.
self.jsondata1 = response.value
// debugPrint(response)
print(jsondata1)
}
}
}
class Testing : Data {
func AnnualTesting() {
//How would I reference jsondata1 here?
debugPrint(jsondata1 ?? "Nothing to see here")
}
}