有人反对吗?没有名为 "removeAtIndex" 的成员

  • 本文关键字:removeAtIndex 成员 swift
  • 更新时间 :
  • 英文 :


我试图从我的tableViewCell中删除一个单元格。我有一个由字符串值组成的列表。然而,AnyObject似乎不包含removeAtIndex。以下是相关代码:

var citiesArray: AnyObject? = nil;
override func viewDidLoad() {
    super.viewDidLoad()
    //loading an array from a file
    var documentList=NSBundle.mainBundle().pathForResource("EuropeanCapitals", ofType:"plist")
    citiesArray = NSArray(contentsOfFile:documentList);
func tableView(tableView: UITableView!, commitEditingStyle editingStyle:        UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        if let tv=tableView
        {
            citiesArray.removeAtIndex(indexPath!.row) /* Throws Error */
            tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
    }

我尝试声明citiesArray为NSArray?我得到NSArray?没有名为'removeAtIndex'的成员。

removeAtIndex()不是NSArray或AnyObject上的方法。你可以尝试把你的NSArray转换成Swift Array,然后调用那个方法。

if var citiesArr = citiesArray as? Array<AnyObject>{
    citiesArr.removeAtIndex(0)
}

removeObjectAtIndex()也可以在NSMutableArray上使用,如果你想使用它的话

最新更新