gpt4 book ai didi

ios - 如何公开我的 Struct 变量?

转载 作者:行者123 更新时间:2023-11-28 08:58:53 24 4
gpt4 key购买 nike

我的问题看起来很简单。但是我挣扎了一天。

我需要将我的结构变量声明为公共(public)变量。以便 UICollectionViewController 数据方法可以访问结构变量。

我该怎么做?我自己试过。但是我做不到

class SkillsController: UICollectionViewController {

var mcnameArray :[String] = []

var mcidArray :[String] = []


func getSkills(){
Alamofire.request(.GET, "http://www.wols.com/index.php/capp/main_category_list")
.responseJSON { (_, _, data, _) in
let json = JSON(data!)
let Count = json.count
for index in 0...Count-1 {
var ds = json[index]["DISPLAY_STATUS"].string
if ds == "Y" {
var mcname = json[index]["MAIN_CATEGORY_NAME"].string
self.mcnameArray.append(mcname!)
var mcid = json[index]["MAIN_CATEGORY_ID"].string
self.mcidArray.append(mcid!)
}

}
var skill = Skills(mcname: self.mcnameArray, mcid: self.mcidArray)
println(skill.mcname.count)
}

}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return skill.mcname.count// Throws an Error
}

最佳答案

您的变量不是“私有(private)”变量,而是局部作用域到定义它的函数。

如果将它移到 var mcnameArrayvar mcidArray 旁边,那么其他函数也可以访问它。

但是,由于您正在发出异步网络请求,因此需要更改该变量。您可能希望将其设为可选 (var skill: Skill?),以便它可以在开始时为 nil,然后再更改。

关于ios - 如何公开我的 Struct 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32392368/

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