Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass enum as parameter to function in objectivec

The header file looks like:

enum RatingsEnum
{
    userRating,
    criticRating,
};

@interface SFMovie : NSObject

- (NSNumber *)getRating:(NSDictionary *)movieDic :(enum RatingsEnum) rating;

@end

How can I use this method getRating? I am not sure how to pass the enum. My calling code:

- (void) testGetCriticRatingMethod{

    NSMutableDictionary *ratingDictionary = [[NSMutableDictionary alloc]init];
    [ratingDictionary setObject:@"Certified Fresh" forKey:@"critics_rating"];
    [ratingDictionary setObject:@"70" forKey:@"critics_score"];
    [ratingDictionary setObject:@"Certified Fresh" forKey:@"audience_rating"];
    [ratingDictionary setObject:@"87" forKey:@"audience_score"];
    SFMovie *movie = [[SFMovie alloc]init];
    enum RatingsEnum ratings;
    NSInteger userRating = [movie getRating:ratingDictionary rating:userRating];
}

This produces the following warning: No visible @interface for 'SFMovie' declares the selector 'getRating:rating:'

Can somebody guide me to a good enum tutorial? Thank you all.

like image 646
U-L Avatar asked Jan 20 '14 23:01

U-L


2 Answers

Change

 - (NSNumber *)getRating:(NSDictionary *)movieDic :(enum RatingsEnum) rating;

to

 - (NSNumber *)getRatingWithDictionary:(NSDictionary *)movieDic ratingEnum:(enum RatingsEnum) ratingEnum;

Change

 enum RatingsEnum ratings;
 NSInteger userRating = [movie getRating:ratingDictionary rating:userRating];

to

 enum RatingsEnum ratings = userRating;
 NSNumber *ratingFromUser = [movie getRatingWithDictionary:ratingDictionary ratingEnum:ratings];
like image 127
Ethan Fang Avatar answered Nov 02 '22 05:11

Ethan Fang


This has nothing to do with the enum, or the type of the parameter at all. Your syntax is simply wrong. The name of the method as declared is getRating::. A correct call looks like

[movie getRating:ratingDictionary :userRating];

Add a label to the second parameter

- (NSNumber *)getRating:(NSDictionary *)movieDic ofType:(enum RatingsEnum)rating;
like image 39
jscs Avatar answered Nov 02 '22 06:11

jscs