/ / Objective C:クラスのget-methodsの問題-objective-c、pointers、parameter-passing

Objective C:class-objective-c、ポインタ、パラメータ渡しのgetメソッドの問題

私はいくつかのクラスがあります。

@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メソッドをサブクラスで使用しようとすると、次のようになります。

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は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;
}