我该怎么做indexOfObject或正确的containsObject [英] How do I do indexOfObject or a proper containsObject
问题描述
使用数组:我怎样做 indexOfObject
或正确的 containsObject
With an array: How do I do indexOfObject
or a proper containsObject
?
我的意思是,我知道我可以只弥合Array对的NSArray
并做到这一点有^^
但必须有这样做的'本土'的方式
I mean I know I could just bridge the Array to NSArray
and do it there ^^
But there must be a 'native' way of doing this
P.S。为 containsObject
我想我可以在阵列筛选过但的indexOf 的?
P.S. for the containsObject
I guess I could filter the array too but for indexOf?
推荐答案
您可以使用内置的找到
,从而避免桥接Objective-C的 - 但只如果你的元素类型是<一个href=\"https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Equatable.html\">Equatable. (如果不是Equatable,可以用一个比较函数和延伸使它左右。)
You can use the built-in find
, and thus avoid bridging to Objective-C — but only if your element type is Equatable. (If it isn't Equatable, you can make it so with a comparison function and an extension.)
例如:
func == (lhs:Piece,rhs:Piece) -> Bool {
return lhs.val == rhs.val
}
class Piece:Equatable,Printable {
var val : Int
var description : String { return String(val) }
init (_ v:Int) {
val = v
}
}
现在你可以叫找到(ARR,P)
,其中改编
是阵列&LT样片方式&gt;
和 p
是片
Now you can call find(arr,p)
where arr
is an Array<Piece>
and p
is a Piece
.
一旦你有了这个,你可以开发基于它的实用程序。例如,这里是一个全球性的功能,以从数组中删除一个对象而桥接Objective-C的:
Once you have this, you can develop utilities based on it. For example, here's a global function to remove an object from an array without bridging to Objective-C:
func removeObject<T:Equatable>(inout arr:Array<T>, object:T) -> T? {
if let found = find(arr,object) {
return arr.removeAtIndex(found)
}
return nil
}
和测试它是这样的:
var arr = [Piece(1), Piece(2), Piece(3)]
removeObject(&arr,Piece(2))
println(arr)
您可以为NSObject的子类做到这一点。例如:
You can do this for NSObject subclasses too. Example:
func == (v1:UIView, v2:UIView) -> Bool {
return v1.isEqual(v2)
}
extension UIView : Equatable {}
现在你可以叫找到
上的UIView数组。这有点在一个痛苦的对接,不过,不必为每一个班级做到这一点,你希望能够使用找到
在该类的一个数组。我已经提交了增强请求与苹果公司要求所有NSObject的子类被认为Equatable和 ==
应该求助于的isEqual:
的自动的
Now you can call find
on an Array of UIView. It's sort of a pain in the butt, though, having to do this for every single class where you want to be able to use find
on an Array of that class. I have filed an enhancement request with Apple requesting that all NSObject subclasses be considered Equatable and that ==
should fall back on isEqual:
automatically.
修改种子3开始,这个的是的自动对的UIView和其他NSObject的类。因此,找到
现在只是为他们工作。
EDIT Starting in Seed 3, this is automatic for UIView and other NSObject classes. So find
now just works for them.
编辑2 在雨燕2.0开始,的indexOf
将存在一个方法:
EDIT 2 Starting in Swift 2.0, indexOf
will exist as a method:
let s = ["Manny", "Moe", "Jack"]
let ix = s.indexOf("Moe") // 1
替代地,它需要一个返回布尔函数:
Alternatively, it takes a function that returns Bool:
let ix2 = s.indexOf {$0.hasPrefix("J")} // 2
同样,这仅适用于Equatable的集合,因为很明显,你无法找到一个大海捞针,除非你有识别针的方式,当你来到它。
Again, this works only on collections of Equatable, since obviously you cannot locate a needle in a haystack unless you have a way of identifying a needle when you come to it.
编辑3 雨燕2.0还引入了协议扩展。这意味着我可以重写我的整体功能的removeObject
作为一种方法!
EDIT 3 Swift 2.0 also introduces protocol extensions. This means I can rewrite my global function removeObject
as a method!
例如:
extension RangeReplaceableCollectionType where Generator.Element : Equatable {
mutating func removeObject(object:Self.Generator.Element) {
if let found = self.indexOf(object) {
self.removeAtIndex(found)
}
}
}
由于采用阵列RangeReplaceableCollectionType,现在我可以写code是这样的:
Since Array adopts RangeReplaceableCollectionType, now I can write code like this:
var arr = [Piece(1), Piece(2), Piece(3)]
arr.removeObject(Piece(2))
呵呵,快乐每一天!
Oh, happy day!
这篇关于我该怎么做indexOfObject或正确的containsObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!