Objective C: Problem with get-methods in class
I have some class:
@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
But when I'm trying to use some these get-methods in subclass, I see:
开发者_如何学Pythonwarning: 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
And program isn't work correctly. What's wrong? How to fix it?
You are passing integer values to your methods which is wrong because the functions declared by you accept only integer pointer
not value that's the reason for having warning.and objectAtIndex:
method accept only integer value not pointer so if you run, it could cause crash in your application.
The simplest solation would be changing the parameter type in your functions.
- (id) getTitleAtIndex:(int )index;
- (id) getUrlAtIndex:(int )index;
- (id) getFlagAtIndex:(int )index;
and function implementaion could be similar to below function.
- (id) getTitleAtIndex:(int )index
{
if(index < [resultsTitles count] )
return [resultsTitles objectAtIndex:index];
else
return nil;
}
精彩评论