gpt4 book ai didi

swift - 在 swift 2.0 中将 NSArray 转换为 NSMutableArray

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

当我尝试将 nsarray 对象插入可变 swift 数组时,我得到了这个控制台输出。代码工作正常,但最后会抛出错误。请帮我解决这个错误。

Successfully retrieved 3 scores.
Optional("HmEbbowtxW")
<Events: 0x7b08bdf0, objectId: HmEbbowtxW, localId: (null)> {
CreatedBy = "<PFUser: 0x7b02ed10, objectId: 04jp1ZeBn6>";
EventDescription = test;
EventName = test;
}
Optional("97BzKUxFdE")
<Events: 0x7b08cae0, objectId: 97BzKUxFdE, localId: (null)> {
CreatedBy = "<PFUser: 0x7b02ed10, objectId: 04jp1ZeBn6>";
EventDescription = fg;
EventName = gfg;
}
Optional("QDHkg5tiUw")
<Events: 0x7b08cf80, objectId: QDHkg5tiUw, localId: (null)> {
CreatedBy = "<PFUser: 0x7b02ed10, objectId: 04jp1ZeBn6>";
EventDescription = asdasdasd;
EventName = sdsd;
}
Could not cast value of type '__NSArrayI' (0x228e164) to 'NSMutableArray' (0x228e1c8).

这是我的代码

 let query = PFQuery(className:"Events")
query.whereKey("CreatedBy", equalTo:PFUser.currentUser()!)

query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in

if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {

self.eventtimelineData.addObject(object)
print(object.objectId)
print(object.description)
}

let array:NSArray = self.eventtimelineData.reverseObjectEnumerator().allObjects
self.eventtimelineData = array as! NSMutableArray
self.tableView.reloadData()

}


} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}

最佳答案

要将 NSArray 转换为 NSMutableArray,请调用 mutableCopy() 并转换为 NSMutableArray:

let a: NSArray = [1, 2.5, "hello"]

let b = a.mutableCopy() as! NSMutableArray

b.addObject(17) // b is [1, 2.5, "hello", 17]

关于swift - 在 swift 2.0 中将 NSArray 转换为 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181824/

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