2013-03-18 1 views
0

모든 목록을 관리하는 Singleton 개체가 있습니다. 우리는 그것을 ListStore라고 부를 것입니다.사용자 지정 개체의 NSCopying 배열

ListStore에는 목록을 저장하는 가변 배열이 있습니다.

@interface ListStore : NSObject 
    @property (nonatomic, copy) NSMutableArray *lists; // an array of List objects 
end 

목록에는 Things가 저장되는 가변 배열이 있습니다. 사용자가 목록과 상호 작용 할 수있는 동안 언제든지

@interface Wanderlist : NSObject <NSCoding, NSCopying> 
    @property (nonatomic, copy) NSMutableArray *things; // an array of Thing objects 
@end 

는 백그라운드 프로세스 및 프로세스의 모든 목록을 통해 ListStore 및 루프를 통해 갈 수 있습니다.

유형의 오류 "를 열거하면서 변이 된 대상"을 방지하기 위해, 나는 이렇게 : 나는 모든 구성원이 제대로 복사 될 것 newLists에 복사본을 만들었 기 때문에

내가 원래 생각했다. 나는 이제 그 경우가 아니라는 것을 이해한다 : 열거 된 동안 "객체가 돌연변이 된"오류를 계속보고 있지만, 이번에는 list.things에서 발생한다.

내가 내 설치를 NSCopying을 사용할 수 있도록 내가 말할 때

[[ListStore sharedStore] copy]; 

그것은 내가 할 수있는 다음 copyWithZone:thingsListscopyWithZone: 호출, 그래서?

이렇게 설정하려고 시도했지만 copyWithZone:이 호출되지 않았습니다.

나는 간단히 NSArray *newList = [list.things copy]라고 말할 수 있지만 적어도 NSCopying에 대해 더 잘 이해하고 싶습니다.

답변

3

이 질문을 제출하기 직전에 나는 SO의 관련 질문 목록에서 질문을 클릭하고 해결책을 찾았습니다.

내 솔루션을 게시하는 데 상처를주지 않습니다. 내가 initWithArray 사용하면

- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag 
flag: 
If YES, each object in array receives a copyWithZone: message to create a copy of the object—objects must conform to the NSCopying protocol. In a managed memory environment, this is instead of the retain message the object would otherwise receive. The object copy is then added to the returned array. 

:이 대신

가 :

NSArray *newLists = [[NSArray alloc] initWithArray:[[ListStore sharedStore] lists] copyItems:true]; 

the NSArray docs에서 :

NSArray *newLists = [[ListStore sharedStore] lists] copy]; 

내가해야 할 일을했을 copyItems을 :, 그것을 자동 판매기 ics 내 모든 목록 개체에 copyWithZone 보낸 및 list.things copyWithZone 수동으로 수행 할 수있었습니다.