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

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

To be fair, it doesn't literally tell that... :P
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.
Thanks - that helped ! I am currently trying to learn swift by porting 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 !
Good luck! Make sure to watch some WWDC sessions, they're a great learning resource for beginners.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.