2015-01-06 110 views
-2

我正在使用switch语句来1)切换复选标记和2)将谓词添加/删除到谓词数组。如果我知道它的名字,但有没有数组中的索引,是否有办法删除一个对象?如果没有,解决方法是什么?这是我的代码的相关部分。在不知道对象索引的情况下从数组中删除对象?

var colorPredicates: [NSPredicate?] = [] 

// Switch statement 
     case blueCell: 
      if (cell.accessoryType == .None) { 
       colorPredicates.append(bluePredicate) 
       cell.accessoryType = .Checkmark 
       println(colorPredicates) // debug code to see what's in there 
      } else { 
       let deleteIndex = find(colorPredicates, bluePredicate) // error: NSPredicate doesn't conform to Equatable. 
       muscleGroupPredicates.removeAtIndex(deleteIndex) 
       cell.accessoryType = .None 
       } 
     default: 
      println("default case") 
+0

找到对象,然后将其删除。如果你将删除多个,则向后索引。没有花哨的代码要求。 –

+0

如何?这是我的问题。 – iOSPadawan

+0

你知道如何使用循环?和“如果”陈述? –

回答

2

已经有一些挫折缺乏斯威夫特方法阵列以及缺乏NSSet概念。你有没有考虑铸造NSArray

var colorPredicates = [NSPredicate]() as NSArray 

colorPredicates.removeObject(bluePredicate) 

另外,我觉得你在你的数据源设计有一个缺陷:你不应该检查电池的accessoryType做其他的东西。这些信息应该在你的数据源中,而不是在一些任意的UI设计元素中。

+0

有问题的视图是带有静态单元格的UITableView,它包含供用户检查的选项。 switch语句更新UI以告诉用户他/她是否选择了谓词并将其从谓词数组中添加/移除。 – iOSPadawan

+0

进一步挖掘,我发现了另一篇文章,你回复了这篇文章,这篇文章重点讨论了我想要做的事情。 http://stackoverflow.com/questions/18157088/ios-compound-predicates – iOSPadawan

-3

我错过了一个'!',它产生了一个编译器错误。

我发现这个职位有帮助:Remove an element in an array without hard-coding the index? in Swift

// Switch statement 
     case blueCell: 
      if (cell.accessoryType == .None) { 
       colorPredicates.append(bluePredicate) 
       cell.accessoryType = .Checkmark 
       println(colorPredicates) // debug code to see what's in there 
      } else { 
       let deleteIndex = find(colorPredicates, bluePredicate) 
       colorPredicates.removeAtIndex(deleteIndex!) 
       cell.accessoryType = .None 
       } 
     //more cases within switch statement 

     default: 
      println("default case") 
相关问题