Skip to content

Commit

Permalink
Merge pull request #349 from hardikdevios/master
Browse files Browse the repository at this point in the history
[NEW] Swift 3 support. [Hardik](#349)
  • Loading branch information
rentzsch authored Oct 3, 2016
2 parents a65f642 + f24fcd4 commit bbefc89
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 214 deletions.
3 changes: 3 additions & 0 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ - (NSString*)objectAttributeClassName {
if (gSwift && [result isEqualToString:@"NSString"]) {
result = @"String";
}
if (gSwift && [result isEqualToString:@"NSDate"]) {
result = @"Date";
}
return result;
}
- (NSArray*)objectAttributeTransformableProtocols {
Expand Down
51 changes: 26 additions & 25 deletions templates/machine.swift.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
<$if Attribute.isReadonly$>
open var <$Attribute.name$>: NSNumber?
{
self.willAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
let <$Attribute.name$> = self.primitiveValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? NSNumber
self.didAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
self.willAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
let <$Attribute.name$> = self.primitiveValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? NSNumber
self.didAccessValue(forKey: <$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
return <$Attribute.name$>
}
<$else$>
Expand All @@ -76,9 +76,9 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
<$if Attribute.isReadonly$>
open var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
{
self.willAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
let <$Attribute.name$> = self.primitiveValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$>
self.didAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
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$>
Expand Down Expand Up @@ -107,26 +107,26 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {

<$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$>) -> 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) -> AnyObject? {
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 = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
"<$Binding.name$>": <$Binding.name$>,
<$endforeach do2$><$endif$>
]

guard let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else {
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: AnyObject? = nil
var result: Any? = nil
do {
let results = try managedObjectContext.executeFetchRequest(fetchRequest)
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.
Expand All @@ -143,23 +143,24 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
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$>) -> [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) -> [AnyObject]? {
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 : AnyObject] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
let substitutionVariables : [String : Any] = [<$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 results = Array<AnyObject>()
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<Any>()
do {
results = try managedObjectContext.executeFetchRequest(fetchRequest!)
results = try managedObjectContext.fetch(fetchRequest)
} catch {
print("Error executing fetch request: \(error)")
}
Expand All @@ -178,25 +179,25 @@ open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
extension _<$sanitizedManagedObjectClassName$> {

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$>) {
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
}

open func add<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) {
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$>) {
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
Expand Down
29 changes: 24 additions & 5 deletions test/MogenSwiftTest/MogenSwiftTest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
796DBE22196F2D9C004EAA15 /* MyEntityMO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796DBE1C196F2D9C004EAA15 /* MyEntityMO.swift */; };
796DBE23196F2D9C004EAA15 /* UnorderedToManyDstMO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796DBE1D196F2D9C004EAA15 /* UnorderedToManyDstMO.swift */; };
796DBE24196F2D9C004EAA15 /* UnorderedToManySrcMO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796DBE1E196F2D9C004EAA15 /* UnorderedToManySrcMO.swift */; };
79F9CECF196F146600D9BF82 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79F9CECE196F146600D9BF82 /* main.swift */; };
79F9CED1196F146600D9BF82 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79F9CED0196F146600D9BF82 /* AppDelegate.swift */; };
79F9CED4196F146600D9BF82 /* MogenSwiftTest.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 79F9CED2196F146600D9BF82 /* MogenSwiftTest.xcdatamodeld */; };
79F9CED6196F146600D9BF82 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 79F9CED5196F146600D9BF82 /* Images.xcassets */; };
Expand All @@ -40,7 +39,6 @@
796DBE1E196F2D9C004EAA15 /* UnorderedToManySrcMO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnorderedToManySrcMO.swift; sourceTree = "<group>"; };
79F9CEC9196F146600D9BF82 /* MogenSwiftTest.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MogenSwiftTest.app; sourceTree = BUILT_PRODUCTS_DIR; };
79F9CECD196F146600D9BF82 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
79F9CECE196F146600D9BF82 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
79F9CED0196F146600D9BF82 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
79F9CED3196F146600D9BF82 /* MogenSwiftTest.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MogenSwiftTest.xcdatamodel; sourceTree = "<group>"; };
79F9CED5196F146600D9BF82 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -103,7 +101,6 @@
isa = PBXGroup;
children = (
79F9CECD196F146600D9BF82 /* Info.plist */,
79F9CECE196F146600D9BF82 /* main.swift */,
);
name = "Supporting Files";
sourceTree = "<group>";
Expand Down Expand Up @@ -183,14 +180,17 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0600;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Jonathan 'Wolf' Rentzsch";
TargetAttributes = {
79F9CEC8196F146600D9BF82 = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0800;
};
79F9CEDD196F146600D9BF82 = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
TestTargetID = 79F9CEC8196F146600D9BF82;
};
};
Expand Down Expand Up @@ -244,7 +244,6 @@
796DBE24196F2D9C004EAA15 /* UnorderedToManySrcMO.swift in Sources */,
796DBE20196F2D9C004EAA15 /* _UnorderedToManyDstMO.swift in Sources */,
796DBE23196F2D9C004EAA15 /* UnorderedToManyDstMO.swift in Sources */,
79F9CECF196F146600D9BF82 /* main.swift in Sources */,
796DBE1F196F2D9C004EAA15 /* _MyEntityMO.swift in Sources */,
79F9CED4196F146600D9BF82 /* MogenSwiftTest.xcdatamodeld in Sources */,
);
Expand Down Expand Up @@ -293,15 +292,19 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -335,8 +338,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -345,6 +350,7 @@
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand All @@ -354,6 +360,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand All @@ -362,9 +369,12 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
INFOPLIST_FILE = MogenSwiftTest/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.rentzsch.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -373,16 +383,20 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
INFOPLIST_FILE = MogenSwiftTest/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.rentzsch.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
79F9CEEC196F146600D9BF82 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/MogenSwiftTest.app/Contents/MacOS/MogenSwiftTest";
CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(DEVELOPER_FRAMEWORKS_DIR)",
Expand All @@ -394,7 +408,9 @@
);
INFOPLIST_FILE = MogenSwiftTestTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.rentzsch.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUNDLE_LOADER)";
};
name = Debug;
Expand All @@ -403,14 +419,17 @@
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/MogenSwiftTest.app/Contents/MacOS/MogenSwiftTest";
CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(DEVELOPER_FRAMEWORKS_DIR)",
"$(inherited)",
);
INFOPLIST_FILE = MogenSwiftTestTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.rentzsch.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUNDLE_LOADER)";
};
name = Release;
Expand Down
Loading

0 comments on commit bbefc89

Please sign in to comment.