Objective c 通过引用传递nsindepath

Objective c 通过引用传递nsindepath,objective-c,ios,automatic-ref-counting,Objective C,Ios,Automatic Ref Counting,我正在使用ARC,希望创建一个通过引用传入indexPath的方法,以便更改其值: -(void)configureIndexPaths:(__bridge NSIndexPath**)indexPath anotherIndexPath:(__bridge NSIndexPath**)anotherIndexPath { indexPath = [NSIndexPath indexPathForRow:*indexPath.row + 1 inSection:0]; a

我正在使用ARC,希望创建一个通过引用传入indexPath的方法,以便更改其值:

-(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的值,那么通过指针传递就不能做到这一点吗?为什么我会选择按引用传递而不是按指针传递?

这就是您要做的:

-(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];
}
您应该使用
\uu自动删除
,以便在创建对象时正确地自动删除对象,并检查传入的
NULL
指针。如果您想要一个真正的
传递引用
,请查看objc++和
nsindepath*&

如果我的目标只是更改传递给方法的
indepath
的值,那么指针传递不能也这样做吗

不太可能,因为索引路径是不可变的。您必须构造一个新的索引路径对象并返回它

为什么我会选择通过引用传递而不是通过指针传递

在ObjC中这样做的唯一真正原因是要有多个返回值。这种技术最常用的用法是有一个返回对象或成功/失败指示器的方法,如果需要,还可以设置错误对象

在本例中,有两个对象要从方法中恢复;一种方法是通过引用传递技巧。这可能会使您的生活变得更简单,可以像现在这样传递两个索引路径,但返回一个带有新路径的
NSArray

 - (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];
}

为什么不让这个方法返回一个新的nsindexpath呢?我正在传递两个唯一的indexPath,我想改变它。NewINDEXPath是一个单独的索引路径。我一直认为双指针是自动删除的,而不是桥…所以如果我通过指针传递,我使用indexPath=[nsindexpath indexPathWith…],那么,这不会更改使用该方法调用的原始索引路径的值?如果它看起来像
(nsindepath**)arg{*arg=[nsindepath indepath…
,那么从调用方的角度来看,这将更改传入的指针。