目标C:类中get-methods的问题

时间:2011-07-09 08:56:34

标签: objective-c pointers parameter-passing

我有一些课程:

@interface SearchBase : NSObject
{
    NSString *words;
NSMutableArray *resultsTitles;
NSMutableArray *resultsUrl;
NSMutableArray *flag;
}

@property (copy, nonatomic) NSString *words;

- (id) getTitleAtIndex:(int *)index;
- (id) getUrlAtIndex:(int *)index;
- (id) getFlagAtIndex:(int *)index;
@end

@implementation SearchBase
 - (id) initWithQuery:(NSString *)words 
{
if (self = [super init])
{
    self.words = words;
}
return self;
}
- (id) getTitleAtIndex:(int *)index
{
return [resultsTitles objectAtIndex:index];
}

- (id) getUrlAtIndex:(int *)index
{
return [resultsUrl objectAtIndex:index];
}

- (id) getFlagAtIndex:(int *)index
{
return [flag objectAtIndex:index];
}
@end

但是当我尝试在子类中使用一些这些get-methods时,我看到了:

warning: passing argument 1 of 'getTitleAtIndex:' makes pointer from integer without a cast 
warning: passing argument 1 of 'getFlagAtIndex:' makes pointer from integer without a cast
warning: passing argument 1 of 'getUrlAtIndex:' makes pointer from integer without a cast

程序无法正常工作。怎么了?如何解决?

1 个答案:

答案 0 :(得分:5)

您正在将整数值传递给您的方法,这是错误的,因为您声明的函数只接受integer pointer而不是值,这是警告和objectAtIndex:方法的原因只接受整数值而非指针,所以如果你运行,可能会导致你的应用程序出现崩溃

最简单的方法是更改​​函数中的参数类型。

- (id) getTitleAtIndex:(int )index;
- (id) getUrlAtIndex:(int )index;
- (id) getFlagAtIndex:(int )index;

和函数实现可能类似于下面的函数。

- (id) getTitleAtIndex:(int )index
{
    if(index < [resultsTitles count] )
        return [resultsTitles objectAtIndex:index];
    else
       return nil;
}