Skip to content

Commit

Permalink
Support set properties to an object by PBMapperProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
galenlin committed Mar 15, 2017
1 parent 8e71783 commit 60a3f85
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Pbind/Classes/Plist/PBMapperProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@

- (BOOL)initPropertiesForOwner:(id)owner; // UIView

/**
Initialize the properties of the owner by KVC
@param owner the owner to be initialized
*/
- (void)initDataForOwner:(id)owner;

/**
Initialize the properties of the owner by setter method
@param owner the owner to be initialized
*/
- (void)setDataToOwner:(id)owner;

- (BOOL)matchesType:(PBMapType)type dataTag:(unsigned char)dataTag;
- (void)mapData:(id)data toTarget:(id)target withContext:(UIView *)context;
- (void)mapData:(id)data toTarget:(id)target forKeyPath:(NSString *)keyPath withContext:(UIView *)context;
Expand Down
7 changes: 7 additions & 0 deletions Pbind/Classes/Plist/PBMapperProperties.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ - (void)initDataForOwner:(id)owner
}
}

- (void)setDataToOwner:(id)owner
{
for (NSString *key in _constants) {
[PBPropertyUtils invokeSetterWithValue:_constants[key] forKey:key toObject:owner failure:nil];
}
}

- (BOOL)matchesType:(PBMapType)type dataTag:(unsigned char)dataTag
{
if (_expressions == nil) {
Expand Down
2 changes: 2 additions & 0 deletions Pbind/Classes/Utils/PBPropertyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@

+ (void)setValuesForKeysWithDictionary:(NSDictionary *)dictionary toObject:(id)object failure:(void (^)(void))failure;

+ (void)invokeSetterWithValue:(id)value forKey:(NSString *)key toObject:(id)object failure:(void (^)(void))failure;

@end
31 changes: 31 additions & 0 deletions Pbind/Classes/Utils/PBPropertyUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,37 @@ + (void)setValue:(id)value forKey:(NSString *)key toObject:(id)object failure:(v
}
}

+ (void)invokeSetterWithValue:(id)value forKey:(NSString *)key toObject:(id)object failure:(void (^)(void))failure
{
BOOL set = NO;
do {
if ([key length] <= 1) {
break;
}

NSString *setterName = [NSString stringWithFormat:@"set%c%@:", toupper([key characterAtIndex:0]), [key substringFromIndex:1]];
SEL setter = NSSelectorFromString(setterName);
if (setter == nil) {
break;
}

IMP imp = [object methodForSelector:setter];
if (imp == nil) {
break;
}

void (*func)(id target, SEL sel, id value) = (void (*)(id, SEL, id)) imp;
func(object, setter, value);
set = YES;
} while (false);

if (!set) {
if (failure) {
failure();
}
}
}

+ (void)setValue:(id)value forKeyPath:(NSString *)keyPath toObject:(id)object failure:(void (^)(void))failure
{
@try {
Expand Down

0 comments on commit 60a3f85

Please sign in to comment.