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

标签 swift parse-platform nsmutablearray

当我尝试将 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/

相关文章:

iOS Swift 移动 FFMpeg 说初始化复杂过滤器时出错。无效的论点

swift - 如何在 swift optional 中比较值

swift - 从条件解析中获取数据

objective-c - 类方法中的 NSMutableArray

objective-c - 在 ios 中添加对象崩溃

ios - 如何在不失真/倾斜的情况下围绕它的适当中心旋转 UIView

swift - 如何解决 Carthage 和 Swift 版本的错误?

javascript - Parse.com cloudcode 从未输入成功/错误

ios - 如何将 iOS Swift 应用程序指向 Amazon Web Services 上的 Parse Server?

ios - 为什么不将值添加到 NSMutableArray?