Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Better mogenerator Swift templates!
import Foundation
@objc(<$managedObjectClassName$>)
public class <$managedObjectClassName$>: _<$managedObjectClassName$> {
// Custom logic goes here.
}
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to <$managedObjectClassName$>.swift instead.
import CoreData
<$if hasAdditionalHeaderFile$>import <$additionalHeaderFileName$><$endif$>
<$if hasCustomBaseCaseImport$>import <$baseClassImport$><$endif$>
@objc public
class _<$managedObjectClassName$>: <$customSuperentity$> {
<$if noninheritedAttributes.@count > 0$>
public struct Attributes {<$foreach Attribute noninheritedAttributes do$>
static let <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$>
}
<$endif$>
<$if noninheritedRelationships.@count > 0$>
public struct Relationships {<$foreach Relationship noninheritedRelationships do$>
static let <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$>
}
<$endif$>
<$if noninheritedFetchedProperties.@count > 0$>
public struct FetchedProperties {<$foreach FetchedProperty noninheritedFetchedProperties do$>
static let <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$>
}
<$endif$>
<$if hasUserInfoKeys$>
public struct UserInfo {<$foreach UserInfo userInfoKeyValues do$>
static let <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$>
}
<$endif$>
// MARK: - Class methods
<$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entityName () -> String {
return "<$name$>"
}
<$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext!) -> NSEntityDescription! {
return NSEntityDescription.entityForName(self.entityName(), inManagedObjectContext: managedObjectContext);
}
// MARK: - Life cycle methods
public override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext!) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
public convenience init(managedObjectContext: NSManagedObjectContext!) {
let entity = _<$managedObjectClassName$>.entity(managedObjectContext)
self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext)
}
// MARK: - Properties
<$foreach Attribute noninheritedAttributes do$>
<$if Attribute.hasDefinedAttributeType$>
<$if Attribute.hasScalarAttributeType$>
<$if Attribute.isReadonly$>
public var <$Attribute.name$>: NSNumber?
{
self.willAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
let <$Attribute.name$> = self.primitiveValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? NSNumber
self.didAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
return <$Attribute.name$>
}
<$else$>
@NSManaged public
var <$Attribute.name$>: NSNumber?
<$endif$>
<$else$>
<$if Attribute.isReadonly$>
public var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
{
self.willAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
let <$Attribute.name$> = self.primitiveValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$>
self.didAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
return <$Attribute.name$>
}
<$else$>
@NSManaged public
var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
<$endif$>
<$endif$>
// func validate<$Attribute.name.initialCapitalString$>(value: AutoreleasingUnsafeMutablePointer<AnyObject>, error: NSErrorPointer) -> Bool {}
<$endif$>
<$endforeach do$>
// MARK: - Relationships
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
@NSManaged public
var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$>
<$else$>
@NSManaged public
var <$Relationship.name$>: <$Relationship.destinationEntity.managedObjectClassName$><$if Relationship.isOptional$>?<$endif$>
// func validate<$Relationship.name.initialCapitalString$>(value: AutoreleasingUnsafeMutablePointer<AnyObject>, error: NSErrorPointer) -> Bool {}
<$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$>) -> AnyObject? {
return self.fetch<$FetchRequest.name.initialCapitalString$>(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) -> AnyObject? {
let model = managedObjectContext.persistentStoreCoordinator!.managedObjectModel
let substitutionVariables = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
"<$Binding.name$>": <$Binding.name$>,
<$endforeach do2$><$endif$> ]
let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables)
assert(fetchRequest != nil, "Can't find fetch request named \"<$FetchRequest.name$>\".")
var error: NSError? = nil
var result: AnyObject? = nil
let results = managedObjectContext.executeFetchRequest(fetchRequest!, error: &error)
if error != nil {
outError.memory = error
} else {
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![0]
default:
NSLog("WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, %lu found (substitutionVariables:%@, results:%@)",
results!.count,
substitutionVariables,
results!);
}
}
return result;
}
<$else$>
class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext!<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> [AnyObject] {
return self.fetch<$FetchRequest.name.initialCapitalString$>(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) -> [AnyObject]! {
let model = managedObjectContext.persistentStoreCoordinator!.managedObjectModel
let substitutionVariables : [NSObject : AnyObject] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
"<$Binding.name$>": <$Binding.name$>,
<$endforeach do2$><$else$>:<$endif$>]
let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables)
assert(fetchRequest != nil, "Can't find fetch request named \"<$FetchRequest.name$>\".")
var error: NSError? = nil
let results = managedObjectContext.executeFetchRequest(fetchRequest!, error: &error)
if error != nil {
outError.memory = error
}
return results
}
<$endif$>
<$endforeach do$>
<$foreach FetchedProperty noninheritedFetchedProperties do$>
@NSManaged public
let <$FetchedProperty.name$>: [<$FetchedProperty.entity.managedObjectClassName$>]
<$endforeach do$>
}
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
extension _<$managedObjectClassName$> {
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<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set(objects<$if !Relationship.jr_isOrdered$> as Set<NSObject><$endif$>)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
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<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set(objects<$if !Relationship.jr_isOrdered$> as Set<NSObject><$endif$>)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
func add<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.managedObjectClassName$>!) {
let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
mutable.addObject(value)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
func remove<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.managedObjectClassName$>!) {
let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
mutable.removeObject(value)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
}
<$endif$><$endforeach do$>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.