diff --git a/mogenerator.h b/mogenerator.h index 06046a3a..79d422dd 100644 --- a/mogenerator.h +++ b/mogenerator.h @@ -20,7 +20,6 @@ - (NSArray*)entitiesWithACustomSubclassVerbose:(BOOL)verbose_; @end - @interface NSEntityDescription (customBaseClass) - (BOOL)hasCustomSuperentity; - (NSString*)customSuperentity; @@ -36,6 +35,15 @@ - (BOOL)hasTransformableAttributeType; @end +@interface NSRelationshipDescription (collectionClassName) +- (NSString*)mutableCollectionClassName; +- (NSString*)immutableCollectionClassName; +- (BOOL)_jr_isOrdered; +@end +@interface NSObject (JustHereToSuppressIsOrderedNotImplementedCompilerWarning) +- (BOOL)isOrdered; +@end + @interface NSString (camelCaseString) - (NSString*)camelCaseString; @end diff --git a/mogenerator.m b/mogenerator.m index 379e41fc..78354fc8 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -261,6 +261,26 @@ - (BOOL)hasTransformableAttributeType { @end +@implementation NSRelationshipDescription (collectionClassName) + +- (NSString*)mutableCollectionClassName { + return [self _jr_isOrdered] ? @"NSMutableOrderedSet" : @"NSMutableSet"; +} + +- (NSString*)immutableCollectionClassName { + return [self _jr_isOrdered] ? @"NSOrderedSet" : @"NSSet"; +} + +- (BOOL)_jr_isOrdered { + if ([self respondsToSelector:@selector(isOrdered)]) { + return [self isOrdered]; + } else { + return NO; + } +} + +@end + @implementation NSString (camelCaseString) - (NSString*)camelCaseString { NSArray *lowerCasedWordArray = [[self wordArray] arrayByMakingObjectsPerformSelector:@selector(lowercaseString)]; diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index 779ad084..dc7eed28 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -35,11 +35,11 @@ <$foreach Relationship noninheritedRelationships do$> <$if Relationship.isToMany$> <$if TemplateVar.arc$> -@property (nonatomic, strong) NSSet* <$Relationship.name$>; +@property (nonatomic, strong) <$Relationship.immutableCollectionClassName$>* <$Relationship.name$>; <$else$> -@property (nonatomic, retain) NSSet* <$Relationship.name$>; +@property (nonatomic, retain) <$Relationship.immutableCollectionClassName$>* <$Relationship.name$>; <$endif$> -- (NSMutableSet*)<$Relationship.name$>Set; +- (<$Relationship.mutableCollectionClassName$>*)<$Relationship.name$>Set; <$else$> <$if TemplateVar.arc$> @property (nonatomic, strong) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>; @@ -65,8 +65,8 @@ @interface _<$managedObjectClassName$> (CoreDataGeneratedAccessors) <$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$> -- (void)add<$Relationship.name.initialCapitalString$>:(NSSet*)value_; -- (void)remove<$Relationship.name.initialCapitalString$>:(NSSet*)value_; +- (void)add<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)value_; +- (void)remove<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)value_; - (void)add<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_; - (void)remove<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_; <$endif$><$endforeach do$> @@ -85,8 +85,8 @@ <$endforeach do$> <$foreach Relationship noninheritedRelationships do$> <$if Relationship.isToMany$> -- (NSMutableSet*)primitive<$Relationship.name.initialCapitalString$>; -- (void)setPrimitive<$Relationship.name.initialCapitalString$>:(NSMutableSet*)value; +- (<$Relationship.mutableCollectionClassName$>*)primitive<$Relationship.name.initialCapitalString$>; +- (void)setPrimitive<$Relationship.name.initialCapitalString$>:(<$Relationship.mutableCollectionClassName$>*)value; <$else$> - (<$Relationship.destinationEntity.managedObjectClassName$>*)primitive<$Relationship.name.initialCapitalString$>; - (void)setPrimitive<$Relationship.name.initialCapitalString$>:(<$Relationship.destinationEntity.managedObjectClassName$>*)value; diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index 709b1fcf..27610405 100644 --- a/templates/machine.m.motemplate +++ b/templates/machine.m.motemplate @@ -26,11 +26,11 @@ return (<$managedObjectClassName$>ID*)[super objectID]; } -+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key { - NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; ++ (<$Relationship.immutableCollectionClassName$> *)keyPathsForValuesAffectingValueForKey:(NSString *)key { + <$Relationship.immutableCollectionClassName$> *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; <$foreach Attribute noninheritedAttributes do$><$if Attribute.hasDefinedAttributeType$><$if Attribute.hasScalarAttributeType$> if ([key isEqualToString:@"<$Attribute.name$>Value"]) { - NSSet *affectingKey = [NSSet setWithObject:@"<$Attribute.name$>"]; + <$Relationship.immutableCollectionClassName$> *affectingKey = [<$Relationship.immutableCollectionClassName$> setWithObject:@"<$Attribute.name$>"]; keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey]; }<$endif$><$endif$><$endforeach do$> @@ -69,9 +69,9 @@ @dynamic <$Relationship.name$>; <$if Relationship.isToMany$> -- (NSMutableSet*)<$Relationship.name$>Set { +- (<$Relationship.mutableCollectionClassName$>*)<$Relationship.name$>Set { [self willAccessValueForKey:@"<$Relationship.name$>"]; - NSMutableSet *result = [self mutableSetValueForKey:@"<$Relationship.name$>"]; + <$Relationship.mutableCollectionClassName$> *result = [self mutableSetValueForKey:@"<$Relationship.name$>"]; [self didAccessValueForKey:@"<$Relationship.name$>"]; return result; }