Skip to content

Commit

Permalink
Merge pull request #4036 from mstepanov/timob-13127
Browse files Browse the repository at this point in the history
[TIMOB-13127] iOS: Ti.App.Properties.setList cannot save arrays that contain null, undefined, or empty values
  • Loading branch information
vishalduggal committed Mar 29, 2013
2 parents fd0596e + dbd632d commit 1748774
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions iphone/Classes/TiAppPropertiesProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#import "TiAppPropertiesProxy.h"
#import "TiUtils.h"

@implementation TiAppPropertiesProxy
@implementation TiAppPropertiesProxy {
NSData *_defaultsNull;
}

-(void)dealloc
{
TiThreadPerformOnMainThread(^{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}, YES);
RELEASE_TO_NIL(defaultsObject);
RELEASE_TO_NIL(_defaultsNull);
[super dealloc];
}

Expand All @@ -43,6 +46,7 @@ -(void)_listenerRemoved:(NSString*)type count:(int)count
-(void)_configure
{
defaultsObject = [[NSUserDefaults standardUserDefaults] retain];
_defaultsNull = [[NSData alloc] initWithBytes:"NULL" length:4];
[super _configure];
}

Expand Down Expand Up @@ -86,7 +90,15 @@ -(id)getString:(id)args
-(id)getList:(id)args
{
GETPROP
return [defaultsObject arrayForKey:key];
NSArray *value = [defaultsObject arrayForKey:key];
NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:[value count]] autorelease];
[(NSArray *)value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSData class]] && [_defaultsNull isEqualToData:obj]) {
obj = [NSNull null];
}
[array addObject:obj];
}];
return array;
}

-(id)getObject:(id)args
Expand Down Expand Up @@ -147,6 +159,16 @@ -(void)setString:(id)args
-(void)setList:(id)args
{
SETPROP
if ([value isKindOfClass:[NSArray class]]) {
NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:[value count]] autorelease];
[(NSArray *)value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSNull class]]) {
obj = _defaultsNull;
}
[array addObject:obj];
}];
value = array;
}
[defaultsObject setObject:value forKey:key];
[defaultsObject synchronize];
}
Expand Down

0 comments on commit 1748774

Please sign in to comment.