CoreData를 사용하는 정적 라이브러리를 작성하면 컴파일 된 버전 (.momd)을 바이너리에 링크 할 수 없기 때문에 일반적인 .xdatamodeld 파일을 포함하여 큰 혼란이 있습니다.NSManagedObjectModel에 관계를 추가하여 프로그래밍 방식으로 만든 NSEntityDescription
NSAttributeDescription *dateAttribute = NSAttributeDescription.new;
dateAttribute.name = @"timestamp";
dateAttribute.attributeType = NSDoubleAttributeType;
dateAttribute.optional = NO;
dateAttribute.indexed = YES;
NSAttributeDescription *payloadAttribute = NSAttributeDescription.new;
payloadAttribute.name = @"payload";
payloadAttribute.attributeType = NSBinaryDataAttributeType;
payloadAttribute.optional = NO;
payloadAttribute.indexed = NO;
NSEntityDescription *entry = NSEntityDescription.new;
entry.name = entry.managedObjectClassName = NSStringFromClass(MyCustomEntry.class);
entry.properties = @[dateAttribute, payloadAttribute];
NSManagedObjectModel *mom = NSManagedObjectModel.new;
mom.entities = @[entry];
을 그리고 모든 것이 완벽하다 ....
그러나 : 그래서 이런 코드의 전체 NSManagedObjectModel
를 작성하는 것이 좋습니다! 잠깐, 내 NSManagedObjectModel
에 엔티티가 두 개 이상 있고 관련이있는 경우 (대다수, 반전 등), 위의 예와 같이 코드에서 Xcode 편집기를 연결하지 않고 어떻게 연결해야하는지 잠깐. 몇 번의 마우스 클릭으로 관계를 맺을 수 있습니까?
상상 예, 우리는 위의 코드에서 MyCustomEntry에서와 거의 동일 클래스 MyCustomElement을 보유하고 있습니다. 지금, 바로 여기에 내가 엔티티 엑스 코드 생성을 사용하는 경우 나타나는 얼마나 자신의 인터페이스 :
@interface MyCustomEntry : NSManagedObject
@property (nonatomic, retain) NSNumber *timestamp;
@property (nonatomic, retain) NSData *payload;
@property (nonatomic, retain) MyCustomElement *element;
@end
@interface MyCustomElement : NSManagedObject
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSString * identifier;
@property (nonatomic, retain) NSSet *entries;
@end
@interface MyCustomElement (CoreDataGeneratedAccessors)
- (void)addEntriesObject:(MyCustomEntry *)value;
- (void)removeEntriesObject:(MyCustomEntry *)value;
- (void)addEntries:(NSSet *)values;
- (void)removeEntries:(NSSet *)values;
@end
내가 그들을 위해 작성해야하는 방법을 초기화하기 위해서 무엇 NSRelationshipDescription
?
예를 들어 업데이트 된 질문을 확인하십시오. – shoumikhin
@shoumikhin : 답변에 샘플 코드를 추가했습니다. –
신난다, 고마워! – shoumikhin