2012-05-12 8 views
2

によって私はARCを使用して、私はその値を変更できるように参照することによりindexPathに渡す方法を作成したいのです:パスNSIndexPath参照

-(void)configureIndexPaths:(__bridge NSIndexPath**)indexPath anotherIndexPath:(__bridge NSIndexPath**)anotherIndexPath 
{ 
     indexPath = [NSIndexPath indexPathForRow:*indexPath.row + 1 inSection:0]; 
     anotherIndexPath = [NSIndexPath indexPathForRow:*anotherIndexPath.row + 1 inSection:0]; 
} 

しかし、これは私にエラーを見つけていないプロパティの行を提供します。どのように私はこれに対処できますか。

もう一つの概念的な質問:もし私の目標が、メソッドに渡されたindexPathの値を変更するだけであれば、ポインタも渡せませんでしたか?ポインタを渡すのではなく、参照渡しを選択するのはなぜですか?

+0

? –

+0

私は変更したい2つのユニークなindexPathを渡しています – Snowman

+0

newIndexPathは別のインデックスパスです – Snowman

答えて

1

これは、あなたがこれを行う方法は以下のようになります

-(void) configureIndexPaths:(NSIndexPath*__autoreleasing *)indexPath anotherIndexPath:(__bridge NSIndexPath*__autoreleasing *)anotherIndexPath 
{ 
    if (indexPath) 
     *indexPath = [NSIndexPath indexPathForRow:[(*indexPath) row] + 1 inSection:0]; 
    if (anotherIndexPath) 
     *anotherIndexPath = [NSIndexPath indexPathForRow:[(*indexPath) row] + 1 inSection:0]; 
} 

あなたは彼らが作成されるときにオブジェクトが適切に自動解放されるように、__autoreleasingを使用し、同様に渡されるNULLポインタをチェックする必要があります場合。本当にpass-by-referenceを望み、objC++とNSIndexPath *&を調べてください。

2

もし私の目標が、メソッドに渡されたindexPathの値を変更するだけであれば、ポインタでもそれを渡せませんでしたか?

インデックスパスが変更可能でないためです。新しいインデックスパスオブジェクトを作成して戻す必要があります。

ポインタを渡すのではなく参照渡しを選択するのはなぜですか?

ObjCでこれを行う本当の理由は、複数の戻り値を持つことだけです。この技法の最も頻繁な使用は、オブジェクトまたは成功/失敗インジケータを返すメソッドを持つことであり、必要に応じてエラーオブジェクトを設定することもできます。

この場合、メソッドから取り戻したいオブジェクトが2つあります。これを行う1つの方法は、参照渡しのトリックです。それはあなたが今のように2つのインデックスのパスを渡すためにあなたの人生をより簡単に、しかし、新しいものとNSArrayを返すことがあります。ちょうどこの方法は、新しいnsindexpathを返していないのはなぜ

- (NSArray *)configureIndexPaths:(NSIndexPath*)indexPath anotherIndexPath:(NSIndexPath*)anotherIndexPath 
{ 
    NSIndexPath * newPath = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:0]; 
    NSIndexPath * anotherNewPath = [NSIndexPath indexPathForRow:[anotherIndexPath row]+1 inSection:0]; 
    return [NSArray arrayWithObjects:newPath, anotherNewPath, nil]; 
} 
+0

従ってポインタを渡してindexPath = [NSIndexPath indexPathWith ...]の場合、メソッドで呼び出された元のインデックスパスの値は変更されませんか? – Snowman

+0

'(NSIndexPath **)arg {* arg = [NSIndexPath indexPath ...]のように見える場合、渡されたポインタは呼び出し元の視点から変更されます。 –

関連する問題