Search code examples
swiftwatchkitfuncwatchos

cannot invoke 'setScore' with an argument list of type '(Int)'


I'm quite new to working with Swift and have a problem: I have two classes in my WatchKit App.

Class 1:

func setScore(inputScore: Int) {
    scoreLabel.setText(String(inputScore))
}

... and Class 2 should have access to this func:

var countNumber = 0
...

self.presentControllerWithName("scorePage", context: self)
            scorePage.setScore(countNumber)

But I always get an error: cannot invoke 'setScore' with an argument list of type '(Int)'

I don't know how to fix this and it would be great if anyone can help me :) Thanks


Solution

  • I’m not sure what are you exactly trying to do but I believe you want to pass data from one WKInterfaceController to other WKInterfaceController, so you can use the context:

    self.presentControllerWithName("scorePage", context: countNumber)

    Once you are in the receiver WKInterfaceController you should receive the countNumber overriding the awakeWithContext: func (the context var should have the countNumber value now) and then you can set the score: self.setScore(context)

    PS don't forget to "cast" the context value from AnyObject to int

    Example using Obj-C and an NSNumber instead of int:

    In the origin class:

    [self pushControllerWithName:@"scorePage" context:countNumber];
    

    in the destination class:

    - (void)awakeWithContext:(id)context {
        [super awakeWithContext:context];
    
        if ([context isKindOfClass:[NSNumber class]]) {
            NSNumber *num = (NSNumber *)context;
           [self setScore:num];
        }
    
    }