嘿计算器的人使用sortedArrayUsingSelector,在斯威夫特
我已经迅速实现了这个类:
class PCCountedColor {
var color:UIColor
var count:Int
init (color:UIColor, count:Int)
{
self.color = color;
self.count = count;
}
func compare(object:PCCountedColor) -> NSComparisonResult
{
if (self.count < object.count)
{
return NSComparisonResult.OrderedDescending
}
else if (self.count == object.count)
{
return NSComparisonResult.OrderedSame
}
return NSComparisonResult.OrderedAscending
}
}
然后,我有这充满以上的类的对象一个NSMutableArray:
var sortedColors:NSMutableArray = []
var container:PCCountedColor = PCCountedColor(color:curColor, count: colorCount)
sortedColors.addObject(container)
之后,我尝试通过上面的类中的特殊功能对数组进行排序:
sortedColors.sortedArrayUsingSelector(Selector("compare:"))
但我得到的是一个错误:
SwiftColorArt[1584:42892] *** NSForwarding: warning: object 0x7fd391b25a50 of class 'SwiftColorArt.PCCountedColor' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[SwiftColorArt.PCCountedColor compare:]
我是新雨燕已经检查Apple's official documentation which can be found here.
我曾尝试多种语法变体(加“:”或将其删除,通函数名称作为一个字符串或不...以及各种组合),但没有一个帮助。
所以在我的绝望中,我转向你寻求帮助。
最好的问候,
月
可能与此处相同的问题:http://stackoverflow.com/questions/24415662/object-x-of-class-y-does-not-implement-methodsignatureforselector-in-swift。 –
你应该***使用[Swift的'sort'函数](https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html#//apple_ref/ DOC/UID/TP40014608-CH5-SW48)。 – akashivskyy