本文介绍了removeObjectsInRange 的 Swift 等效项:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在跟踪 Swift 等效项时遇到一些麻烦:
Having a little trouble tracking down the Swift equivalent of:
//timeArray and locationArray are NSMutableArrays
NSRange removalRange = NSMakeRange(0, i);
[timeArray removeObjectsInRange:removalRange];
[locationArray removeObjectsInRange:removalRange];
我看到 Swift 在 API 中确实有一个调用:typealias NSRange = _NSRange
但我还没有通过那部分.有什么帮助吗?
I see that Swift does have a call in the API: typealias NSRange = _NSRange
but I haven't got past that part. Any help?
推荐答案
除了 Antonio 的回答,您还可以只使用范围运算符:
In addition to Antonio's answer, you can also just use the range operator:
var array = [0, 1, 2, 3, 4, 5]
array.removeRange(1..<3)
// array is now [0, 3, 4, 5]
- 半闭区间运算符 (
1..) 包括 1,最多但不包括 3(所以 1-2).
- 全范围运算符 (
1...3
) 包括 3(即 1-3). - The half-closed range operator (
1..<3
) includes 1, up to but not including 3 (so 1-2). - A full range operator (
1...3
) includes 3 (so 1-3).
这篇关于removeObjectsInRange 的 Swift 等效项:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!