Skip to content

Commit fc99ca4

Browse files
committed
Make mogenerator:readonly work for Swift by rendering the attribute as a read-only computed property.
1 parent 43f61d0 commit fc99ca4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

templates/machine.swift.motemplate

+14-4
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,26 @@ class _<$managedObjectClassName$>: <$customSuperentity$> {
5858
<$if Attribute.hasDefinedAttributeType$>
5959
<$if Attribute.hasScalarAttributeType$>
6060
<$if Attribute.isReadonly$>
61-
@NSManaged public
62-
let <$Attribute.name$>: NSNumber?
61+
public var <$Attribute.name$>: NSNumber?
62+
{
63+
self.willAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
64+
let <$Attribute.name$> = self.primitiveValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? NSNumber
65+
self.didAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
66+
return <$Attribute.name$>
67+
}
6368
<$else$>
6469
@NSManaged public
6570
var <$Attribute.name$>: NSNumber?
6671
<$endif$>
6772
<$else$>
6873
<$if Attribute.isReadonly$>
69-
@NSManaged public
70-
let <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
74+
public var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
75+
{
76+
self.willAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
77+
let <$Attribute.name$> = self.primitiveValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$>
78+
self.didAccessValueForKey(<$managedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
79+
return <$Attribute.name$>
80+
}
7181
<$else$>
7282
@NSManaged public
7383
var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>

0 commit comments

Comments
 (0)