diff --git a/README.md b/README.md index 91584a43..52b5bb91 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,30 @@ Senseful wrote up a [nice summary of mogenerator's command-line options](http:// ## Version History +### v1.33: Date TBD + +* [NEW] Update Swift templates for better Swift style ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/368)) + +* [FIX] Fixed invalid call of `NSManagedObject.fetchRequest()` ([Vincent Esche](https://github.com/rentzsch/mogenerator/pull/372)) + +* [NEW] Add support for URI and UUID attribute types ([Trevor Squires](https://github.com/rentzsch/mogenerator/pull/370)) + +* [NEW] Expose information when custom type is set through userInfo ([Aleksandar Vacić](https://github.com/rentzsch/mogenerator/pull/369)) + +* [NEW] Add Codable property support via a new attributeCodableTypeName user info key ([Tom Harrington](https://github.com/rentzsch/mogenerator/issues/375)) + +* [CHANGE] Nullability annotations for generated primitive accessors ([Michael Babin](https://github.com/rentzsch/mogenerator/pull/363)) + +* [FIX] Support for “Uses Scalar Type” ([Rok Gregorič](https://github.com/rentzsch/mogenerator/pull/352)) + +* [FIX] Swift 3 `NSData` to `Data` ([Christopher Rogers](https://github.com/rentzsch/mogenerator/pull/350)) + +* [FIX] Add specialized `fetchRequest()` func to Swift's machine generated files ([0xpablo](https://github.com/rentzsch/mogenerator/pull/358)) + +* [FIX] Correct Swift machine template for singleton (fetchOne…) fetch request results ([Warren Burton](https://github.com/rentzsch/mogenerator/pull/359)) + +* [FIX] Expose allAttributes and allRelationships ([Trevor Squires](https://github.com/rentzsch/mogenerator/pull/360)) + ### v1.32: Wed Jan 30 2019 [download](https://github.com/rentzsch/mogenerator/releases/tag/1.32) * [NEW] Support for URL and UUID property types ([Trevor Squires](https://github.com/tomekc/mogenerator/pull/1), [original PR](https://github.com/rentzsch/mogenerator/pull/370)) diff --git a/mogenerator.m b/mogenerator.m index 752844e7..f6c8eaf1 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -16,6 +16,7 @@ static const NSString *const kAttributeValueScalarTypeKey = @"attributeValueScalarType"; static const NSString *const kAdditionalHeaderFileNameKey = @"additionalHeaderFileName"; +static const NSString *const kUsesRawValueEnumType = @"attributeRawValueEnumType"; static const NSString *const kAdditionalImportsKey = @"additionalImports"; static const NSString *const kCustomBaseClass = @"mogenerator.customBaseClass"; static const NSString *const kReadOnly = @"mogenerator.readonly"; @@ -457,6 +458,15 @@ - (BOOL)hasScalarAttributeType { } } +- (BOOL)usesRawValueEnumType { + NSNumber *usesRawValueEnumType = [[self userInfo] objectForKey:kUsesRawValueEnumType]; + return (usesRawValueEnumType != NULL); +} + +- (NSString *)attributeRawValueEnumType { + return [[self userInfo] objectForKey:kUsesRawValueEnumType]; +} + - (BOOL)usesScalarAttributeType { NSNumber *usesScalarAttributeType = [[self userInfo] objectForKey:kUsesScalarAttributeType]; @@ -586,6 +596,13 @@ - (NSString*)objectAttributeClassName { if (!result) { result = gSwift ? @"AnyObject" : @"NSObject"; } + } else if (gSwift && [self attributeType] == NSBinaryDataAttributeType) { + NSString *codableName = [self userInfo][@"attributeCodableTypeName"]; + if (!codableName) { + result = @"Data"; + } else { + result = codableName; + } } else { // Forcibly generate the correct class name in case we are // running on macOS < 10.13 @@ -638,6 +655,10 @@ - (BOOL)usesCustomObjectAttributeType { return (attributeValueClassName != nil); } +- (BOOL)usesCustomCodableAttributeType { + return [self userInfo][@"attributeCodableTypeName"] != NULL; +} + - (NSString*)objectAttributeType { NSString *result = [self objectAttributeClassName]; if ([result isEqualToString:@"Class"]) { @@ -1063,7 +1084,7 @@ - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { } if (_version) { - printf("mogenerator 1.32. By Jonathan 'Wolf' Rentzsch + friends.\n"); + printf("mogenerator 1.33. By Jonathan 'Wolf' Rentzsch + friends.\n"); return EXIT_SUCCESS; } diff --git a/templates/human.swift.motemplate b/templates/human.swift.motemplate index 3abb1aa5..881757b3 100644 --- a/templates/human.swift.motemplate +++ b/templates/human.swift.motemplate @@ -1,6 +1,10 @@ import Foundation +import CoreData + +@objc(<$sanitizedManagedObjectClassName$>) +open class <$sanitizedManagedObjectClassName$>: <$customSuperentity$> { + + // Put custom code for <$sanitizedManagedObjectClassName$> here. + // This file will be created if it doesn't exist, but mogenerator will not replace an existing copy. -@objc(<$managedObjectClassName$>) -open class <$managedObjectClassName$>: _<$managedObjectClassName$> { - // Custom logic goes here. } diff --git a/templates/machine.swift.motemplate b/templates/machine.swift.motemplate index f423728e..37cdb0e3 100644 --- a/templates/machine.swift.motemplate +++ b/templates/machine.swift.motemplate @@ -1,217 +1,183 @@ -// DO NOT EDIT. This file is machine-generated and constantly overwritten. -// Make changes to <$sanitizedManagedObjectClassName$>.swift instead. +// DO NOT EDIT. This file is generated by mogenerator and will be overwritten. +// Make changes to <$managedObjectClassName$>.swift instead. -import Foundation import CoreData <$if hasCustomBaseCaseImport$>import <$baseClassImport$><$endif$> <$if hasAdditionalImports$> +// Additional imports via the "additionalImports" userInfo key. <$foreach Import additionalImports do$> import <$Import$> <$endforeach do$> <$endif$> -<$if noninheritedAttributes.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>Attributes: String {<$foreach Attribute noninheritedAttributes do$> - case <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$> -} -<$endif$> - -<$if noninheritedRelationships.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>Relationships: String {<$foreach Relationship noninheritedRelationships do$> - case <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$> -} -<$endif$> - -<$if noninheritedFetchedProperties.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>FetchedProperties: String {<$foreach FetchedProperty noninheritedFetchedProperties do$> - case <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$> -} -<$endif$> -<$if hasUserInfoKeys && userInfoKeyValues.@count > 0$> -public enum <$sanitizedManagedObjectClassName$>UserInfo: String {<$foreach UserInfo userInfoKeyValues do$> - case <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> -} -<$endif$> - - -<$if hasCustomSuperentity $> -open class _<$sanitizedManagedObjectClassName$>: <$customSuperentity$> { -<$else$> -open class _<$sanitizedManagedObjectClassName$>: NSManagedObject { -<$endif$> - // MARK: - Class methods +public extension <$managedObjectClassName$> { - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entityName () -> String { + // Convenience lookup of the entity name for <$managedObjectClassName$> + @objc <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class var entityName: String { return "<$name$>" } - <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { - return NSEntityDescription.entity(forEntityName: self.entityName(), in: managedObjectContext) - } - - @nonobjc - open class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> { - return NSFetchRequest(entityName: self.entityName()) + // Convenience to create a new <$managedObjectClassName$> in a managed object context. + @objc <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { + return NSEntityDescription.entity(forEntityName: entityName, in: managedObjectContext) } + + <$if noninheritedAttributes.@count > 0$> + // Attribute names for <$managedObjectClassName$> + public struct Attributes {<$foreach Attribute noninheritedAttributes do$> + static let <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$> + } + <$endif$> - // MARK: - Life cycle methods + <$if noninheritedRelationships.@count > 0$> + // Core Data relationship names for <$managedObjectClassName$> + public struct Relationships {<$foreach Relationship noninheritedRelationships do$> + static let <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$> + } + <$endif$> - public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) { - super.init(entity: entity, insertInto: context) - } + <$if noninheritedFetchedProperties.@count > 0$> + // Fetched propertie names for <$managedObjectClassName$> + public struct FetchedProperties {<$foreach FetchedProperty noninheritedFetchedProperties do$> + static let <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$> + } + <$endif$> - public convenience init?(managedObjectContext: NSManagedObjectContext) { - guard let entity = _<$sanitizedManagedObjectClassName$>.entity(managedObjectContext: managedObjectContext) else { return nil } - self.init(entity: entity, insertInto: managedObjectContext) + <$if hasUserInfoKeys && userInfoKeyValues.@count > 0$> + public struct UserInfo {<$foreach UserInfo userInfoKeyValues do$> + static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$> + } + <$endif$> + /// Create a fetch request for this entity. + public class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> { + return NSFetchRequest(entityName: self.entityName) } - + // MARK: - Properties <$foreach Attribute noninheritedAttributes do$> -<$if Attribute.hasDefinedAttributeType$> +<$if Attribute.hasDefinedAttributeType$> <$if Attribute.hasScalarAttributeType$> -<$if Attribute.isReadonly$> - open var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$if Attribute.isOptional$> // Optional scalars not supported<$endif$><$else$>NSNumber<$if Attribute.isOptional$>?<$else$>!<$endif$><$endif$> - { - self.willAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - let <$Attribute.name$> = self.primitiveValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> - self.didAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - return <$Attribute.name$> - } -<$else$> - @NSManaged open - var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$if Attribute.isOptional$> // Optional scalars not supported<$endif$><$else$>NSNumber<$if Attribute.isOptional$>?<$else$>!<$endif$><$endif$> -<$endif$> -<$else$> -<$if Attribute.isReadonly$> - open var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$else$>!<$endif$> - { - self.willAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - let <$Attribute.name$> = self.primitiveValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$> - self.didAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) - return <$Attribute.name$> - } -<$else$> - @NSManaged open - var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$else$>!<$endif$> -<$endif$> -<$endif$> -<$endif$> -<$endforeach do$> + +<$if Attribute.isOptional$> +<$if Attribute.usesRawValueEnumType$> + // Attribute <$Attribute.name$> is an enum with a numeric raw type. + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + guard let primitiveValue = primitiveValue(forKey: key) as? <$Attribute.attributeRawValueEnumType$>.RawValue else { return nil } + return <$Attribute.attributeRawValueEnumType$>(rawValue: primitiveValue) + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + guard let value = newValue else { + setPrimitiveValue(nil, forKey: key) + return + } + setPrimitiveValue(value.rawValue, forKey: key) + } + } +<$elseif Attribute.usesScalarAttributeType$> + // Attribtue <$Attribute.name$> is a scalar optional + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.scalarAttributeType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + return primitiveValue(forKey: key) as? <$Attribute.scalarAttributeType$> + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + guard let value = newValue else { + setPrimitiveValue(nil, forKey: key) + return + } + setPrimitiveValue(value, forKey: key) + } + } +<$else$> <$comment isOptional = true but the above checks are not true $> + @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: NSNumber? +<$endif$> <$comment end of if/elesif/else for optional properties $> +<$else$> <$comment isOptional is not true but hasScalarAttributeType is still true $> + // <$Attribute.name$> is required (not optional) + @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$if Attribute.usesScalarAttributeType$><$Attribute.scalarAttributeType$><$else$>NSNumber<$endif$> +<$endif$> <$comment isOptional $> +<$else$> <$comment hasScalarAttributeType = false (object type) $> +<$if Attribute.usesRawValueEnumType$> <$comment This is here because enums with String raw value don't count as scalars $> + // Attribute <$Attribute.name$> is an enum with a non-numeric raw type. + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.attributeRawValueEnumType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + guard let primitiveValue = primitiveValue(forKey: key) as? <$Attribute.attributeRawValueEnumType$>.RawValue else { return nil } + return <$Attribute.attributeRawValueEnumType$>(rawValue: primitiveValue) + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + guard let value = newValue else { + setPrimitiveValue(nil, forKey: key) + return + } + setPrimitiveValue(value.rawValue, forKey: key) + } + } +<$elseif Attribute.usesCustomCodableAttributeType$> + // Attribute <$Attribute.name$> with Codable type <$Attribute.objectAttributeType$> + public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$>? { + get { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willAccessValue(forKey: key) + defer { didAccessValue(forKey: key) } + + guard let primitiveValue = primitiveValue(forKey: key) as? Data else { + return nil + } + + return try? JSONDecoder().decode(<$Attribute.objectAttributeType$>.self, from: primitiveValue) + } + set { + let key = <$managedObjectClassName$>.Attributes.<$Attribute.name$> + willChangeValue(forKey: key) + defer { didChangeValue(forKey: key) } + + let primitiveValue = try? JSONEncoder().encode(newValue) + setPrimitiveValue(primitiveValue, forKey: key) + } + } +<$else$> <$comment usesRawValueEnumType = false $> + @NSManaged public<$if Attribute.isReadonly$> private(set)<$endif$> var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> +<$endif$> <$comment usesRawValueEnumType $> +<$endif$> <$comment hasScalarAttributrType$> +<$endif$> <$comment hasDefinedAttributeType$> +<$endforeach do$> <$comment end of properties $> // MARK: - Relationships <$foreach Relationship noninheritedRelationships do$> <$if Relationship.isToMany$> - @NSManaged open - var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$> - - open func <$Relationship.name$>Set() -> <$Relationship.mutableCollectionClassName$> { - return self.<$Relationship.name$>.mutableCopy() as! <$Relationship.mutableCollectionClassName$> - } - -<$else$> - @NSManaged open - var <$Relationship.name$>: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$><$if Relationship.isOptional$>?<$endif$> -<$endif$> -<$endforeach do$> - -<$foreach FetchRequest prettyFetchRequests do$> -<$if FetchRequest.singleResult$> - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> Any? { - return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil) - } - - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> Any? { - guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil } - let model = psc.managedObjectModel - let substitutionVariables : [String : Any] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$> - "<$Binding.name$>": <$Binding.name$>, - <$endforeach do2$><$else$>:<$endif$>] - - guard let fetchRequest = model.fetchRequestFromTemplate(withName: "<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else { - assert(false, "Can't find fetch request named \"<$FetchRequest.name$>\".") - return nil - } - - var result: Any? = nil - do { - let results = try managedObjectContext.fetch(fetchRequest) - switch results.count { - case 0: - // Nothing found matching the fetch request. That's cool, though: we'll just return nil. - break - case 1: - result = results.first - default: - print("WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, \(results.count) found (substitutionVariables: \(substitutionVariables), results: \(results))") - } - - } catch { - print("Error executing fetch request: \(error)") - } - return result - } + @NSManaged public var <$Relationship.name$>: Set<<$Relationship.destinationEntity.managedObjectClassName$>><$if Relationship.isOptional$>?<$endif$> <$else$> - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> [Any]? { - return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil) - } - - class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> [Any]? { - guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil } - let model = psc.managedObjectModel - let substitutionVariables : [String : Any] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$> - "<$Binding.name$>": <$Binding.name$>, -<$endforeach do2$><$else$>:<$endif$>] - - guard let fetchRequest = model.fetchRequestFromTemplate(withName: "<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else { - assert(false, "Can't find fetch request named \"<$FetchRequest.name$>\".") - return nil - } - var results = Array() - do { - results = try managedObjectContext.fetch(fetchRequest) - } catch { - print("Error executing fetch request: \(error)") - } - - return results - } + @NSManaged public var <$Relationship.name$>: <$Relationship.destinationEntity.managedObjectClassName$><$if Relationship.isOptional$>?<$endif$> <$endif$> <$endforeach do$> <$foreach FetchedProperty noninheritedFetchedProperties do$> - @NSManaged open - let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] + @NSManaged public let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] <$endforeach do$> -} - -<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$> -extension _<$sanitizedManagedObjectClassName$> { - - open func add<$Relationship.name.initialCapitalString$>(_ objects: <$Relationship.immutableCollectionClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.union(objects<$if !Relationship.jr_isOrdered$> as Set<$endif$>) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } - - open func remove<$Relationship.name.initialCapitalString$>(_ objects: <$Relationship.immutableCollectionClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.minus(objects<$if !Relationship.jr_isOrdered$> as Set<$endif$>) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } - - open func add<$Relationship.name.initialCapitalString$>Object(_ value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.add(value) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } - - open func remove<$Relationship.name.initialCapitalString$>Object(_ value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) { - let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - mutable.remove(value) - self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set - } } -<$endif$><$endforeach do$> diff --git a/test/swift/main.swift b/test/swift/main.swift index 75d6e568..9f281937 100644 --- a/test/swift/main.swift +++ b/test/swift/main.swift @@ -23,38 +23,38 @@ struct CoreDataStore { let dataStore = CoreDataStore() let moc = dataStore.moc // -let homer = ParentMO(managedObjectContext: moc)! +let homer = ParentMO(context: moc) homer.humanName = "homer" homer.parentName = homer.humanName homer.ivar = 1.0 homer.gender = NSNumber(value: Gender.Male.rawValue) -let marge = ParentMO(managedObjectContext: moc)! +let marge = ParentMO(context: moc) marge.humanName = "marge" marge.parentName = marge.humanName marge.ivar = 1.0 marge.gender = NSNumber(value: Gender.Female.rawValue) -assert(homer.children.count == 0) -assert(marge.children.count == 0) +assert(homer.children!.count == 0) +assert(marge.children!.count == 0) -let bart = ChildMO(managedObjectContext: moc)! +let bart = ChildMO(context: moc) bart.humanName = "bart" bart.childName = bart.humanName bart.ivar = 1.0 bart.type = 64 -let lisa = ChildMO(managedObjectContext: moc)! +let lisa = ChildMO(context: moc) lisa.humanName = "lisa" lisa.childName = lisa.humanName lisa.ivar = 1.0 do { try moc.save() - assert(Gender(rawValue: homer.gender!.intValue) == .Male) - assert(Gender(rawValue: marge.gender!.intValue) == .Female) - assert(Gender(rawValue: bart.gender!.intValue) == .Undefined) - assert(Gender(rawValue: homer.gender!.intValue)!.toString() == "Male") + assert(Gender(rawValue: homer.gender.intValue) == .Male) + assert(Gender(rawValue: marge.gender.intValue) == .Female) + assert(Gender(rawValue: bart.gender.intValue) == .Undefined) + assert(Gender(rawValue: homer.gender.intValue)!.toString() == "Male") } catch { assertionFailure("Failed to save")