Skip to content

Commit 084a6d0

Browse files
Merge pull request #81 from ParsePlatform/richardross.propertyinfo.thingywingy
Added weak/assign value support for PFPropertyInfo, and improved @synthesize detection.
2 parents 8c0d65c + 76f8e9d commit 084a6d0

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

Parse/Internal/PropertyInfo/PFPropertyInfo.m

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,24 @@ - (instancetype)initWithClass:(Class)kls name:(NSString *)propertyName
6262
_ivar = class_getInstanceVariable(kls, [safeStringWithPropertyAttributeValue(objcProperty, "V") UTF8String]);
6363
if (_ivar) break;
6464

65-
// If we implement a property in a subclass with a different type then the parent, but rely upon the parent's
66-
// implementation, we will have to attempt to infer the variable name...
67-
// TODO: (richardross): Walk the superclass tree for the synthesized value?
65+
// Walk the superclass heirarchy for the property definition. Because property attributes are not inherited
66+
// (but property definitions *are*), we must be careful to ensure that the variable was never actually
67+
// implemented and synthesized in a superclass. Note if the same property is synthesized in multiple classes
68+
// with different iVars, we take the class furthest from the root class as the 'source of truth'.
69+
Class superClass = class_getSuperclass(kls);
70+
while (superClass) {
71+
objc_property_t superProperty = class_getProperty(superClass, [_name UTF8String]);
72+
if (!superProperty) break;
73+
74+
_ivar = class_getInstanceVariable(superClass, [safeStringWithPropertyAttributeValue(superProperty, "V") UTF8String]);
75+
if (_ivar) break;
76+
77+
superClass = class_getSuperclass(kls);
78+
}
79+
80+
if (_ivar) break;
81+
82+
// Attempt to infer the variable name.
6883
_ivar = class_getInstanceVariable(kls, [[@"_" stringByAppendingString:_name] UTF8String]);
6984
if (_ivar) break;
7085

@@ -86,11 +101,19 @@ - (instancetype)initWithClass:(Class)kls name:(NSString *)propertyName
86101
_setterSelector = NSSelectorFromString(propertySetter);
87102

88103
if (_associationType == PFPropertyInfoAssociationTypeDefault) {
89-
// TODO: (richardross) Check if the property is weak as well.
90104
BOOL isCopy = safeStringWithPropertyAttributeValue(objcProperty, "C") != nil;
91-
_associationType = (_object ? (isCopy ? PFPropertyInfoAssociationTypeCopy
92-
: PFPropertyInfoAssociationTypeStrong)
93-
: PFPropertyInfoAssociationTypeAssign);
105+
BOOL isWeak = safeStringWithPropertyAttributeValue(objcProperty, "W") != nil;
106+
BOOL isRetain = safeStringWithPropertyAttributeValue(objcProperty, "&") != nil;
107+
108+
if (isWeak) {
109+
_associationType = PFPropertyInfoAssociationTypeWeak;
110+
} else if (isCopy) {
111+
_associationType = PFPropertyInfoAssociationTypeCopy;
112+
} else if (isRetain) {
113+
_associationType = PFPropertyInfoAssociationTypeStrong;
114+
} else {
115+
_associationType = PFPropertyInfoAssociationTypeAssign;
116+
}
94117
}
95118

96119
return self;

0 commit comments

Comments
 (0)