2011-07-09 95 views
0

获取的方法,我有一些类:目标C:问题与

@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 

但是,当我试图使用在子类中一些获得的方法,我看到:

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 

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

+3

仅供参考,可可约定对这些方法不使用“get”前缀。只需使用'titleAtIndex:'和'urlAtIndex:'之类的东西。 –

回答

5

你传入整数值到你的方法它,因为你声明的函数只接受integer pointer并不看重这对有warning.and 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; 
} 
+0

作为@ shaggy上面说的,这是一种错误..它应该是 - (id)titleAtIndex:(int)i;或 - (void)getTitle:(NSString *)atIndex:(int)index; – hooleyhoop