[英]How to cast an array as NSMutableArray to be able to use removeObjectAtIndex
I have an array of users which I'm trying to cast as NSMutableArray
to be able to use removeObjectAtIndex
, but it doesn't work. 我有一组用户试图将其转换为
NSMutableArray
,以便能够使用removeObjectAtIndex
,但它不起作用。 I get either the error Cast from '[ModelUser]?' to unrelated type 'NSMutableArray' always fails
我收到
Cast from '[ModelUser]?' to unrelated type 'NSMutableArray' always fails
的错误Cast from '[ModelUser]?' to unrelated type 'NSMutableArray' always fails
Cast from '[ModelUser]?' to unrelated type 'NSMutableArray' always fails
if I try to cast it, or '[ModelUser]' does not have a member named 'removeObjectAtIndex'
if I don't. Cast from '[ModelUser]?' to unrelated type 'NSMutableArray' always fails
如果我尝试将其'[ModelUser]' does not have a member named 'removeObjectAtIndex'
Cast from '[ModelUser]?' to unrelated type 'NSMutableArray' always fails
,或者'[ModelUser]' does not have a member named 'removeObjectAtIndex'
。
So how could I remove an object from my array in this code? 那么如何在这段代码中从数组中删除一个对象呢?
query.friend(friendId, command: command, completion: { (result:Bool) -> () in
if result {
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
if let friendR = self.friendRequests as? NSMutableArray{
friendR.removeObjectAtIndex(indexPath.row)
}
println("Friendship deleted")
})
You can't turn an NSArray
into an NSMutableArray
simply by casting it - they are different classes. 您不能仅通过强制转换将
NSArray
转换为NSMutableArray
它们是不同的类。 To get a mutable array from an NSArray
you can invoke the mutableCopy
method on the NSArray. 要从
NSArray
获取可变数组,可以在NSArray上调用mutableCopy
方法。
However, in your case the array is a Swift array, so it is already mutable. 但是,在您的情况下,该数组是Swift数组,因此它已经可变。
You need to use removeAtIndex
not removeObjectAtIndex
to remove an object from a Swift array - 您需要使用
removeAtIndex
而不是removeObjectAtIndex
来从Swift数组中删除对象-
query.friend(friendId, command: command, completion: { (result:Bool) -> () in
if result {
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
if self.friendRequests != nil {
self.friendRequests!.removeAtIndex(indexPath.row)
}
println("Friendship deleted")
})
Actually you are doing a type casting only. 实际上,您仅在进行类型转换。 You need to make
friendR
mutable to delete and add any object. 您需要使
friendR
可变以删除和添加任何对象。
if let friendR = self.friendRequests.mutableCopy() as? NSMutableArray{
friendR.removeObjectAtIndex(indexPath.row)
}
mutableCopy()
and a new Instance of NSMutableArray
would work. mutableCopy()
和新的NSMutableArray
实例将起作用。
you should use the following code to directly cast for example NSMutableArray to [YourType] 您应该使用以下代码将NSMutableArray例如直接转换为[YourType]
if var friendR = self.friendRequests as? AnyObject as? [ModelUser] {
// your swift array handling code here
}
I used var
keyword to enable future friendR
mutations 我使用了
var
关键字来启用将来的friendR
突变
您应该从一开始self.friendRequests
初始化为NSMutableArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.