gpt4 book ai didi

ios - 无法将对象添加到 NSMutableArray

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

大家好,我正在尝试 Swift 和 Parse,也许你可以在这里给我提示,我不知道为什么我的 NSMutableArray typeAccessory 总是空的,没有对象,我正在尝试从 Parse 添加对象。

[编辑]

我正在尝试使用 XLForm 做一个选择器操作表,操作表需要一个元素数组,我想将对象从解析传递到“row.selectorOptions”作为一个数组。

//XL表格

row = XLFormRowDescriptor(tag :Tags.ActionSheet.rawValue, rowType:XLFormRowDescriptorTypeSelectorActionSheet, title:"Sheet")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
row.value = "Option 3"
section.addFormRow(row)

有什么想法吗?

谢谢

   override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: "submit:")
self.loadAccessoryType() //loading function
}

func loadAccessoryType()-> NSMutableArray{
var typeAccessory:NSMutableArray? = NSMutableArray()
var query = PFQuery(className:"accessory_type")
query.selectKeys(["type"])
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects{
typeAccessory?.addObject(object["type"]!)
}
}
}else{
println("No results")
}
}
return typeAccessory!
}

最佳答案

findObjectsInBackgroundWithBlock 将异步工作,因此该方法将跳过此代码块并直接返回 return typeAccessory,它始终是一个空数组。考虑使用 findObjects 方法,或者如果您坚持异步执行此操作,请尝试使用回调:

func loadAccessoryType(){
var typeAccessory : NSMutableArray = NSMutableArray()
var query = PFQuery(className:"accessory_type")
query.selectKeys(["type"])
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects{
typeAccessory.addObject(object["type"]!)
}
self.onAddingObjectsCompleted(typeAccessory)
}
}else{
self.onAddingObjectsFailed()
}
}

func onAddingObjectsCompleted(data : NSMutableArray) {
//stuff to do when its done
}

func onAddingObjectsFailed() {
//stuff to do when it failed
}

关于ios - 无法将对象添加到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367398/

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