Ios 使用Swift中的选择器进行分拣
嘿,斯塔克沃夫的人们 我已经在swift中实现了这个类:Ios 使用Swift中的选择器进行分拣,ios,sorting,swift,nsarray,unrecognized-selector,Ios,Sorting,Swift,Nsarray,Unrecognized Selector,嘿,斯塔克沃夫的人们 我已经在swift中实现了这个类: class PCCountedColor { var color:UIColor var count:Int init (color:UIColor, count:Int) { self.color = color; self.count = count; } func compare(object:PCCountedColor) -> NSCom
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
}
}
之后,我尝试通过上述类中的特殊函数对该数组进行排序:
var sortedColors:NSMutableArray = []
var container:PCCountedColor = PCCountedColor(color:curColor, count: colorCount)
sortedColors.addObject(container)
sortedColors.sortedArrayUsingSelector(Selector("compare:"))
但我得到的只是一个错误:
SwiftColorArt[1584:42892]***NSForwarding:警告:对象
“SwiftColorArt.PCCountedColor”类的0x7fd391b25a50不可用
实现方法SignatureForSelector:--无法识别前方故障
选择器-[SwiftColorArt.PCCountedColor比较:]
我是Swift新手,已经检查过了
我尝试过几种语法变体(添加“:”或删除它们,将函数名作为字符串传递或不传递…以及各种组合),但都没有任何帮助
所以我在绝望中向你求助
致以最良好的祝愿
一月
“SwiftColorArt.PCCountedColor'未实现methodSignatureForSelector:--故障预兆无法识别的选择器-[SwiftColorArt.PCCountedColor compare:”
错误消息告诉您该怎么做。将该类设为NSObject的子类,一切都会好起来。可能与此处的问题相同:。您应该真正使用。公平地说,它并没有确切地说明…:波凯,嗯,它告诉我该怎么做。:)但实际上,我同意你的看法,@akashivsky-如果可以避免的话,OP首先不应该调用这个方法。Swift拥有优秀的本地阵列排序设备。谢谢-这很有帮助!我目前正在尝试通过将(用Objective-C编写,我也不知道)移植到swift来学习swift@阿卡西夫斯基-我一定会调查的!祝你好运一定要看一些WWDC课程,它们对初学者来说是很好的学习资源。@Janggregortriebel你所做的和我学习时做的一样。首先,我将我现有的应用程序从Objective-C翻译成Swift。然后我重新组装了它们,使它们更快速。