1

Hey people of stackoverflow,

I have implemented this class in swift:

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
    }
}

Then I have an NSMutableArray which is being filled with objects of above class:

var sortedColors:NSMutableArray = []
var container:PCCountedColor = PCCountedColor(color:curColor, count: colorCount)
sortedColors.addObject(container)

After which I try to have that array sorted via a special function in the above class:

sortedColors.sortedArrayUsingSelector(Selector("compare:"))

But all I get is an error:

SwiftColorArt[1584:42892] *** NSForwarding: warning: object 0x7fd391b25a50 of class 'SwiftColorArt.PCCountedColor' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[SwiftColorArt.PCCountedColor compare:]

I am new to Swift and have already checked Apple's official documentation which can be found here.

I have tried several syntax variants (adding ":" or remove them, pass the function name as a string or not ... as well as various combinations) but none of them helped.

So in my desperation I turn to you for help.

Best regards,

Jan

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
Jan
  • 907
  • 1
  • 8
  • 23
  • Probably the same issue as here: http://stackoverflow.com/questions/24415662/object-x-of-class-y-does-not-implement-methodsignatureforselector-in-swift . – Martin R Jan 07 '15 at 19:38
  • You should ***really*** be using [Swift's `sort` function](https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html#//apple_ref/doc/uid/TP40014608-CH5-SW48). – akashivskyy Jan 07 '15 at 19:59

1 Answers1

3

"SwiftColorArt.PCCountedColor' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[SwiftColorArt.PCCountedColor compare:]"

The error message tells you what to do. Make this class a subclass of NSObject and all will be well.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • To be fair, it doesn't *literally* tell that... :P – akashivskyy Jan 07 '15 at 20:00
  • Okay, well, it tells _me_ what to do. :) But really, I agree with you, @akashivskyy - the OP should not be calling this method in the first place, if it can be avoided. Swift has excellent native facilities for sorting an array. – matt Jan 07 '15 at 20:02
  • Thanks - that helped ! I am currently trying to learn swift by porting https://github.com/fleitz/ColorArt (which is written in Objective-C, which I also don't know) to swift. @akashivskyy - I will make sure to look into that ! – Jan Jan 07 '15 at 20:11
  • Good luck! Make sure to watch some WWDC sessions, they're a great learning resource for beginners. – akashivskyy Jan 07 '15 at 20:13
  • @JanGregorTriebel What you're doing is the same sort of thing I did when I was learning. First, I translated my existing apps very literally from Objective-C to Swift. Then I rearchitected them to make them more Swifty. – matt Jan 07 '15 at 20:14
  • @JanGregorTriebel And translating from Objective-C to Swift without really knowing Objective-C is also a very good idea, because you will often need to do that. – matt Jan 07 '15 at 20:15
  • Thanks for the pep talk guys ! Just in time :-) – Jan Jan 07 '15 at 20:15