2013-08-18 3 views
4

정말 열거 형 몇 가지 "정수 32"특성 가진 NSManagedObject 하위 있습니다. 이 열거 형은이처럼 내 모델의 .H 파일에 정의되어 있습니다 :내 응용 프로그램에서 NSZombie를 찾았습니다 ... 이제 어떻게해야합니까?

typedef enum { 
    AMOwningCompanyACME, 
    AMOwningCompanyABC, 
    AMOwningCompanyOther 
} AMOwningCompany; 

내가,이 사용자 정의 개체의 각 속성의 값을 표시하는 테이블보기를 표시해야하는 각 열거에 대해 나는처럼 보이는 방법을 그래서 이 문자열 값 반환 : 내 테이블보기에서

-(NSArray*)stringsForAMOwningCompany 
{ 
    return [NSArray arrayWithObjects:@"ACME Co.", @"ABC Co.", @"Other", nil]; 
} 

을 내 NSManagedObject (사용 NSEntityDescriptionattributesByName의 속성을 반복하고 각 속성에 대해 나는 적절한 "stringsFor"메소드를 호출하는 도우미 메서드를 호출 해당 특정 속성에 대한 문자열을 반환하십시오.

-(NSArray*)getStringsArrayForAttribute:(NSString*)attributeName 
{ 
    SEL methodSelector = NSSelectorFromString([self methodNameForAttributeNamed:attributeName]); 
    NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:[[AMProperty class] instanceMethodSignatureForSelector:methodSelector]]; 
    [invocation setSelector:methodSelector]; 
    [invocation setTarget:self.editingPole]; 
    [invocation invoke]; 

    NSArray* returnValue = nil; 
    [invocation getReturnValue:&returnValue]; 

    return returnValue; 
} 

내 테이블보기의 cellForRowAtIndexPath는 다음과 같습니다

하나의 속성에 대한
... 
NSString* itemName = self.tableData[indexPath.row]; 
NSAttributeDescription* desc = itemAttributes[itemName]; 

NSString* cellIdentifier = [self cellIdentifierForAttribute:desc]; // checks the attribute type and returns a different custom cell identifier accordingly 
if ([cellIdentifier isEqualToString:@"enumCell"]) 
{ 
    // dequeue cell, customize it 
    UITableViewCell* enumCell = ... 
    ... 
    NSArray* stringValues = [[NSArray alloc] initWithArray:[self getStringArrayForAttribute:itemName]]; 
    int currentValue = [(NSNumber*)[self.editingPole valueForKey:itemName] intValue]; 
    enumCell.detailTextLabel.text = (NSString*)stringValues[currentValue]; 
    return enumCell; 
} 
... 

, 나는 NSInvocation의 반환 배열에 충돌 점점 계속 :

-[__NSArrayI release]: message sent to deallocated instance 0x856a4b0 

은 Using를 좀비 프로필러, 참조 :

Instruments Screenshot

저는 ARC를 사용하고 있습니다. 어떻게 디버깅 할 수 있습니까?

답변

8

필자는 최근에 매우 비슷한 문제를 겪었으며, 내가 잘못하고있는 것이 무엇인지 알아 내려고 노력했습니다. returnValue 포인터가 __strong (기본값)이므로 ARC는 해당 포인터를 통해 객체가 소유되고 있다고 생각하지만 그렇지 않습니다.

-[NSInvocation getReturnValue:]은 소유권을 갖지 않으며 포인터의 주소를 통한 "할당"은 ARC가 일반적으로 사용하는 objc_storeStrong()을 우회합니다.

해결책은 간단합니다. 포인터를 __unsafe_unretained으로 표시하십시오. 이것은 진리입니다. 이 포인터를 통해 객체가 유지되지 않습니다 (예 : __strong 일 경우). 그러면 ARC가 여기에서 포인터를 놓아서는 안됩니다.

+0

놀라움. 고맙습니다. Objective-C에서 메모리 관리를 완전히 이해하지 못했으며 ARC와 함께 걱정할 필요가 없다고 생각했습니다. 분명히 사실이 아닙니다;) – Shinigami

+0

보통 당신은하지 않지만'void * '를 통해 객체를 전달하는 것은 미치광이 프린지와 같습니다. –