2011-09-14 23 views
14

Tôi đang cố chuyển con trỏ tới con trỏ tới phương thức, nhưng dường như ARC có một số vấn đề với cách tôi đang thực hiện nó. Đây là hai phương pháp:Vấn đề đếm tham chiếu tự động: Chuyển địa chỉ của đối tượng không phải cục bộ tới tham số __autoreleasing để ghi lại

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

tôi nhận được lỗi sau:

Automatic Reference Counting Issue: Passing address of non-local object to __autoreleasing parameter for write-back

Trên dòng mà lệnh sau đây xuất hiện:

[self SHL_initPersonPropertyNamesWithArray:&propertyNames] : NULL; 
+0

Kiểm tra [https://stackoverflow.com/questions/8814718/handling-pointer-to-pointer-ownership-issues-in-arc?answertab=active#tab-top] này, điều này sẽ làm rõ hầu hết nghi ngờ – tharinduNA

Trả lời

26

Các vòng loại lưu trữ __strong là cần thiết cho trường hợp này .

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

Tuy nhiên, mã này không tuân theo Basic Memory Management Rules.

You own any object you create

You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

Vì lý do gì bạn muốn làm điều này?

+0

Vâng, thay vì sử dụng IF có điều kiện mỗi khi phương pháp này được gọi, tôi sử dụng phép toán ba giây theo sau hàm khởi tạo, nhưng có lẽ tốt hơn là tôi sẽ gắn bó với những gì quen thuộc trong thế giới Objective-C thay vì C. – Rami

Các vấn đề liên quan