在我的应用程序中,我在选择单元格时在数组中添加了一个对象,并在重新选择单元格时取消选择并删除对象.我使用了那段代码但是给了我错误.
extension Array {
func indexOfObject(object : AnyObject) -> NSInteger {
return (self as NSArray).indexOfObject(object)
}
mutating func removeObject(object : AnyObject) {
for var index = self.indexOfObject(object); index != NSNotFound; index = self.indexOfObject(object) {
self.removeAtIndex(index)
}
}
}
class MyViewController: UITableViewController {
var arrContacts: [Any] = []
var contacts: [Any] = []
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
arrContacts.removeObject(contacts[indexPath.row])
}
}
Run Code Online (Sandbox Code Playgroud)
它给了我2个错误:
C-style for statement has been removed in Swift 3
Value of type '[Any]' has no member …Run Code Online (Sandbox Code Playgroud) 我有以下一段代码:
class C {
private enum E {
// ...
}
}
private extension C {
func f(e: E) { // error: Method must be declared private because its parameter uses a private type
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用错误方法private,编译器错误就会消失。我想知道这是 Swift 中的错误还是我没有得到什么?
我试图从字符串数组的后面删除""& ,直到最后一项包含一些文本,但我的实现没有拾取." "" "
到目前为止我的实现:
var array = ["A", "B", "", "C", "D", " ", " ", ""]
while true {
if (array.last == " " || array.last == "") {
array.removeLast()
} else {
break
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的输出是
["A", "B", "", "C", "D"]
Run Code Online (Sandbox Code Playgroud)
,但我当前的输出是
["A", "B", "", "C", "D", " ", " "]
Run Code Online (Sandbox Code Playgroud)
,其中在遇到后while立即循环breaks" "
有什么建议为什么它不接听吗" "?