Jan
Jan

Reputation: 935

Using sortedArrayUsingSelector in Swift

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

Upvotes: 1

Views: 2757

Answers (1)

matt
matt

Reputation: 535850

"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.

Upvotes: 3

Related Questions