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

Better optional property behavior and other enhancements to Swift support #245

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions mogenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
@interface NSObject (JustHereToSuppressIsOrderedNotImplementedCompilerWarning)
- (BOOL)isOrdered;
@end
@interface NSPropertyDescription (swiftOptionality)
- (NSString*)swiftOptionality;
@end

@interface NSString (camelCaseString)
- (NSString*)camelCaseString;
Expand Down
46 changes: 46 additions & 0 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,52 @@ - (BOOL)jr_isOrdered {

@end

@implementation NSPropertyDescription (swiftOptionality)

- (NSString*)swiftOptionality {
if(self.optional) {
return @"?";
}
else {
return @"";
}
}

@end

@implementation NSAttributeDescription (swiftOptionality)

- (NSString *)swiftOptionality {
if(!self.optional && !self.defaultValue) {
// This property will start out as nil, so let's make it an implicit optional.
return @"!";
}
else {
return [super swiftOptionality];
}
}

@end

@implementation NSRelationshipDescription (swiftOptionality)

- (NSString *)swiftOptionality {
if (self.isToMany) {
// To-many relationships become "optional" by being empty, not by being nil.
return @"";
}
else {
NSString * optionality = [super swiftOptionality];
if([optionality isEqualToString:@""]) {
// This relationship will start off as nil, so let's make it an implicit optional.
optionality = @"!";
}
return optionality;
}
}

@end

@implementation NSString (camelCaseString)
- (NSString*)camelCaseString {
NSArray *lowerCasedWordArray = [[self wordArray] arrayByMakingObjectsPerformSelector:@selector(lowercaseString)];
Expand Down
2 changes: 2 additions & 0 deletions templates/human.swift.motemplate
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import CoreData

@objc(<$managedObjectClassName$>)
class <$managedObjectClassName$>: _<$managedObjectClassName$> {

Expand Down
16 changes: 11 additions & 5 deletions templates/machine.swift.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ class _<$managedObjectClassName$>: <$customSuperentity$> {
<$if Attribute.hasScalarAttributeType$>
<$if Attribute.isReadonly$>
@NSManaged
let <$Attribute.name$>: NSNumber?
let <$Attribute.name$>: NSNumber<$Attribute.swiftOptionality$>
<$else$>
@NSManaged
var <$Attribute.name$>: NSNumber?
var <$Attribute.name$>: NSNumber<$Attribute.swiftOptionality$>
<$endif$>
<$else$>
<$if Attribute.isReadonly$>
@NSManaged
let <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
let <$Attribute.name$>: <$Attribute.objectAttributeType$><$Attribute.swiftOptionality$>
<$else$>
@NSManaged
var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
var <$Attribute.name$>: <$Attribute.objectAttributeType$><$Attribute.swiftOptionality$>
<$endif$>
<$endif$>
// func validate<$Attribute.name.initialCapitalString$>(value: AutoreleasingUnsafePointer<AnyObject>, error: NSErrorPointer) {}
Expand All @@ -81,9 +81,15 @@ class _<$managedObjectClassName$>: <$customSuperentity$> {
@NSManaged
var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$>

var <$Relationship.name$>Set: <$Relationship.mutableCollectionClassName$> {
willAccessValueForKey("<$Relationship.name$>")
let ret = mutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>SetValueForKey("<$Relationship.name$>")
didAccessValueForKey("<$Relationship.name$>")
return ret
}
<$else$>
@NSManaged
var <$Relationship.name$>: <$Relationship.destinationEntity.managedObjectClassName$><$if Relationship.isOptional$>?<$endif$>
var <$Relationship.name$>: <$Relationship.destinationEntity.managedObjectClassName$><$Relationship.swiftOptionality$>

// func validate<$Relationship.name.initialCapitalString$>(value: AutoreleasingUnsafePointer<AnyObject>, error: NSErrorPointer) {}
<$endif$>
Expand Down