objective-c - 通过引用传递 NSIndexPath

标签 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];
      anotherIndexPath = [NSIndexPath indexPathForRow:*anotherIndexPath.row + 1 inSection:0];
}

但这给了我一个属性行未找到错误。我该如何解决这个问题。

还有另一个概念性问题:如果我的目标只是更改传递给方法的 indexPath 的值,那么通过指针传递是否也能做到这一点?为什么我选择通过引用传递而不是通过指针传递?

最佳答案

if my my goal is just to change the value of indexPath that was passed in to the method, couldn't passing by pointer also do that?

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

Why would I choose to pass by reference rather than pass by pointer?

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

关于objective-c - 通过引用传递 NSIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565031/

相关文章:

java - Swift 或 Objective C 中的 java ByteBuffer 等价于什么?

swift ,两个问题。 1) weak var 2) @IBOutlet 的 bang 运算符

objective-c - AssetsLibrary 没有按预期产生故障

c# - 来自 C# 背景的哪个更容易学习 : Objective C or Java?

objective-c - 需要解析一个字符串到没有填充 0 的日期

ios - 打开 Apple map 进行导航并设置目的地名称

ios - 方法可能缺少 [super dealloc] 调用

objective-c - 使用为 ARC 配置的 XMLRPC 库,收到 ARC 错误

javascript - osx 多线程和 webview

ios - 如何从braintree payments中获取卡号?