Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Swift to-many accessors, Swift mutableSets #389

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions templates/machine.swift.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
return NSEntityDescription.entity(forEntityName: self.entityName(), in: managedObjectContext)
}

@nonobjc
open class func fetchRequest() -> NSFetchRequest<<$sanitizedManagedObjectClassName$>> {
return NSFetchRequest(entityName: self.entityName())
}

// MARK: - Life cycle methods

public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
Expand Down Expand Up @@ -107,9 +102,15 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$>

open func <$Relationship.name$>Set() -> <$Relationship.mutableCollectionClassName$> {
return self.<$Relationship.name$>.mutableCopy() as! <$Relationship.mutableCollectionClassName$>
willAccessValue(forKey: "<$Relationship.name$>")
<$if Relationship.jr_isOrdered$>
let result = mutableOrderedSetValue(forKey:"<$Relationship.name$>")
<$else$>
let result = mutableSetValue(forKey:"<$Relationship.name$>")
<$endif$>
didAccessValue(forKey: "<$Relationship.name$>");
return result
}

<$else$>
@NSManaged open
var <$Relationship.name$>: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$><$if Relationship.isOptional$>?<$endif$>
Expand Down Expand Up @@ -188,30 +189,16 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {

<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
extension _<$sanitizedManagedObjectClassName$> {
@NSManaged
open func add<$Relationship.name.initialCapitalString$>(_ objects: <$Relationship.immutableCollectionClassName$>)

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<NSObject><$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<NSObject><$endif$>)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
@NSManaged
open func remove<$Relationship.name.initialCapitalString$>(_ objects: <$Relationship.immutableCollectionClassName$>)

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
}
@NSManaged
open func add<$Relationship.name.initialCapitalString$>Object(_ value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>)

@NSManaged
open func remove<$Relationship.name.initialCapitalString$>Object(_ value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>)
}
<$endif$><$endforeach do$>