作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在遍历一个对象数组,如果它与特定的项目列表匹配,我想删除该项目。在循环完成对数组的迭代之前,我无法删除该项目。所以我想存储一个指向该对象的临时指针,然后在它完成迭代后将其删除。
public func deleteItem(item: NSDictionary) {
for object in self.objects() {
if object .isEqualToDictionary(item as! [String : String]) {
//Store pointer to matched item
}
}
//Delete matched item
}
我将如何做到这一点?提前致谢。
最佳答案
使用 .enumerate()
的简单 for 循环索引应该可以实现您的目标。
public func deleteItem(item: NSDictionary) {
var deletionIndex = 0 //Initialize an index with wider scope.
for object in self.objects().enumerate() { //Add .enumerate()
//Add .element to get the object and .index for the index
if object.element.isEqualToDictionary(item as! [String : String]) {
//Store index to matched item instead of a pointer
deletionIndex = object.index
}
}
//Delete matched item
self.objects().removeAtIndex(deletionIndex)
}
我觉得这实际上应该可以通过过滤操作轻松实现:
public func deleteItem(item: NSDictionary) {
self.objects = self.objects.filter{ $0 != item }
}
这当然会删除项目的所有实例,而不仅仅是最后一个已知索引
关于swift - 如何存储指向对象的指针并稍后引用它以从数组中删除项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34754941/
我正在编写一个插件,有一个ajax调用向用户显示数据。 如果用户想在ajax成功时添加一些js?他可以从他的 js 脚本中做到这一点吗,比如定位这个 ajax 成功事件。 例如: $(documen
我有 html 代码,例如 - x 最初插入 div 'insert_calendar_eda_form'。 Javascript代码 calendar_eda_add
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我已经使用命令 sudo start myservice 启动了一个 upstart 服务。我想要一种方法,以便稍后我(或任何人)可以检查该服务是否已启动并正在运行。检查它的命令是什么? 最佳答案 找
我是一名优秀的程序员,十分优秀!