【问题标题】:Automatic Reference Counting Issue: Passing address of non-local object to __autoreleasing parameter for write-back自动引用计数问题:将非本地对象的地址传递给 __autoreleasing 参数以进行回写
【发布时间】:2011-11-16 22:27:23
【问题描述】:

我正在尝试将指针传递给指向方法的指针,但显然 ARC 对我的操作方式存在一些问题。这里有两种方法:

+ (NSString *)personPropertyNameForIndex:(kSHLPersonDetailsTableRowIndex)index 
{
    static NSArray *propertyNames = nil;

    (nil == propertyNames) ? 
        [self SHL_initPersonPropertyNamesWithArray:&propertyNames] : NULL;
}

+ (void)SHL_initPersonPropertyNamesWithArray:(NSArray **)theArray
{
    *theArray = [[NSArray alloc] 
                 initWithObjects:@"name", @"email", @"birthdate", @"phone", nil];
}

我收到以下错误:

自动引用计数问题:将非本地对象的地址传递给 __autoreleasing 参数以进行回写

在出现以下命令的那一行:

[self SHL_initPersonPropertyNamesWithArray:&propertyNames] : NULL;

【问题讨论】:

标签: objective-c pointers ios5 automatic-ref-counting


【解决方案1】:

这种情况需要 __strong 存储限定符。

+ (void)SHL_initPersonPropertyNamesWithArray:(NSArray * __strong *)theArray

但是,此代码不遵循Basic Memory Management Rules

您拥有您创建的任何对象

您使用名称以“alloc”、“new”、“copy”或“mutableCopy”开头的方法创建对象(例如,alloc、newObject 或 mutableCopy)。

你为什么要这样做?

【讨论】:

  • 好吧,不是每次调用此方法时都使用条件 IF,而是使用三元运算,然后是初始化函数,但也许我会坚持使用 Objective-C 中熟悉的内容。世界而不是 C。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
相关资源
最近更新 更多