gpt4 book ai didi

ios - 生成字典函数的问题

转载 作者:行者123 更新时间:2023-11-28 11:40:26 25 4
gpt4 key购买 nike

有这样一个函数……

func getCricketerList() {
presenter?.cricketerListDidFetch(cricketerList: getAllCricketerDetail())
}

在这里,getAllCricketerDetail() 给出为...

func getAllCricketerDetail() -> [Cricketer] {
var cricketerList = [Cricketer]()
let allCricketerDetail = Common.generateCricketerList()
for cricketer in allCricketerDetail {
cricketerList.append(Cricketer(attributes: cricketer))
}
return cricketerList
}

Common.generateCricketerList() 是这样给出的……

class Common: NSObject {

class func generateCricketerList() -> [[String: String]] {
return [["name": "Sachin Tendulkar","country":"India"],["name":"Brett Lee","country":"Australia"],["name":"Wasim Akram","country":"Pakistan"],["name":"Sourav Ganguly","country":"India"],["name":"Jonty Rhodes","country":"South Africa"]]
}
}

这工作正常。现在我正在其他地方尝试实现类似的功能。为此,我定义了一个名为 Common2 的类。现在,我想要在这个类中有 3 个键/值对,即名称、描述和图像。我已经这样给了……

class Common2: NSObject {

func generateCricketerDetailList() -> [[String:Any]] {
return [["name": "Sachin Tendulkar","description":"add description here","image": UIImage(named: "Sachin.jpeg")!],["name":"Sourav Ganguly","description":"add description here","image":UIImage(named: "sourav.jpeg")!],["name":"Adam Gilchrist","description":"add description here","image":UIImage(named: "adam.jpeg")!],["name":"Jonty Rhodes","description":"add description here","image":UIImage(named: "adam.jpeg")!],["name":"Wasim Akram","description":"add description here","image":UIImage(named: "wasim.jpeg")!]]
}
}

现在,当我尝试在函数中实现 Common2 时,我得到了这样的建议…… enter image description here

现在,如果我单击突出显示的选项,我会得到类似这样的内容...

enter image description here

我该怎么办...?我在这里做错了什么...?

另外,我在 Common2 中定义的函数 generateCricketerDetailList() 是否正确...?

最佳答案

你忘了让它像另一个一样成为一个类函数......

class func generateCricketerDetailList() -> [[String:Any]] {
return [["name": "Sachin Tendulkar","description":"add description here","image": UIImage(named: "Sachin.jpeg")!],["name":"Sourav Ganguly","description":"add description here","image":UIImage(named: "sourav.jpeg")!],["name":"Adam Gilchrist","description":"add description here","image":UIImage(named: "adam.jpeg")!],["name":"Jonty Rhodes","description":"add description here","image":UIImage(named: "adam.jpeg")!],["name":"Wasim Akram","description":"add description here","image":UIImage(named: "wasim.jpeg")!]]
}

与问题无关,我建议您将信息存储在结构中而不是字典中。除非您为了这个问题而试图模仿 api 响应。

关于ios - 生成字典函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53827103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com