简体   繁体   中英

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.

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];

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM