如何将enum作为参数传递给目标c中的函数

时间:2014-01-20 23:40:29

标签: objective-c enums

头文件如下所示:

enum RatingsEnum
{
    userRating,
    criticRating,
};

@interface SFMovie : NSObject

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

@end

如何使用此方法getRating?我不知道如何通过枚举。我的主叫代码:

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

这会产生以下警告: 'SFMovie'没有可见的@interface声明选择器'getRating:rating:'

有人可以指导我一个好的enum教程吗?谢谢大家。

2 个答案:

答案 0 :(得分:17)

更改

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

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

更改

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

 enum RatingsEnum ratings = userRating;
 NSNumber *ratingFromUser = [movie getRatingWithDictionary:ratingDictionary ratingEnum:ratings];

答案 1 :(得分:1)

这与enum或参数的类型完全无关。你的语法完全错了。声明的方法名称为getRating::。正确的通话看起来像

[movie getRating:ratingDictionary :userRating];

为第二个参数添加标签

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