我正在编写一个很好的类来处理NSUserDefaults
. 还没有测试过,但我想我错过了一些关于 update/add by 的东西callBlockID
。我的代码:
我的代码:
@implementation UserDefaultHandler
+ (UserDefaultHandler *)sharedInstance
{
static UserDefaultHandler *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[UserDefaultHandler alloc] init];
});
return instance;
}
-(void)updateOrAddCallBlock:(NSMutableDictionary*)callBlockObject
{
NSString *callBlockID = [callBlockObject objectForKey:@"callBlockID"];
BOOL needToUpdate = NO;
NSInteger indexDict = 0;
NSMutableArray *arrayOfAllCallBlock = [[NSMutableArray alloc] init];
arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayOfAllCallBlock"];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
if (arrayOfAllCallBlock.count <= 0 || arrayOfAllCallBlock == nil)
{
[arrayOfAllCallBlock addObject:callBlockObject];
}
else
{
for (int i = 0; i < arrayOfAllCallBlock.count; i++)
{
NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
NSString *idFromDict = [mutableDict objectForKey:@"callBlockID"];
if ([idFromDict isEqualToString:callBlockID])
{
needToUpdate = YES;
indexDict = i;
newDict = mutableDict;
}
}
if (needToUpdate)
{
[arrayOfAllCallBlock removeObjectAtIndex:indexDict];
[arrayOfAllCallBlock insertObject:newDict atIndex:indexDict];
}
else
{
[arrayOfAllCallBlock addObject:callBlockObject];
}
}
[self saveGlobalDict:arrayOfAllCallBlock];
}
- (NSMutableDictionary *)getCallBlockByID:(NSString*)callBlockID
{
NSMutableArray *arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayOfAllCallBlock"];
BOOL isFound = NO;
NSInteger indexDict = 0;
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
for (int i = 0; i < arrayOfAllCallBlock.count; i++)
{
NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
NSString *idFromDict = [mutableDict objectForKey:@"callBlockID"];
if ([idFromDict isEqualToString:callBlockID])
{
isFound = YES;
indexDict = i;
newDict = mutableDict;
}
}
if (isFound)
{
return newDict;
}
else
{
return nil;
}
}
- (NSMutableArray*)getAllCallBlock
{
NSMutableArray *arrayOfAllCallBlock = (NSMutableArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayOfAllCallBlock"];
return arrayOfAllCallBlock;
}
- (void)clearCallBlockDB
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"arrayOfAllCallBlock"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)saveGlobalDict:(NSMutableArray*)globalArray
{
self.globalArray = [[NSMutableArray alloc] init];
self.globalArray = globalArray;
[self clearCallBlockDB];
[[NSUserDefaults standardUserDefaults] setObject:self.globalArray forKey:@"arrayOfAllCallBlock"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)clearSingleCallBlock:(NSString*)callBlockID
{
NSMutableArray *array = [[NSMutableArray alloc] init];
array = [self getAllCallBlock];
NSMutableDictionary *specificDict = [[NSMutableDictionary alloc] init];
NSInteger getIndex = [self getIndexForCallBlock:array specificDict:specificDict];
[array removeObjectAtIndex:getIndex];
[self saveGlobalDict:array];
}
- (NSInteger)getIndexForCallBlock:(NSMutableArray*)arrayOfAllCallBlock specificDict:(NSMutableDictionary*)specificDict
{
NSInteger getIndex = 0;
for (int i = 0; i < arrayOfAllCallBlock.count; i++)
{
NSMutableDictionary *mutableDict = [arrayOfAllCallBlock objectAtIndex:i];
NSDictionary *dict_one = [specificDict copy];
NSDictionary *dict_two = [mutableDict copy];
if ([dict_two isEqualToDictionary:dict_one])
{
getIndex = i;
}
}
return getIndex;
}
@end
我将解释:我有一个 UITableView,当我向它添加一行时,我有一个新窗口来放置详细信息,保存并返回 UITableView。另外,我有一个编辑按钮,因此我可以编辑和更新表格中的一行。向表中添加一行会创建一个唯一的字符串 (callBlockID)。我不确定如何处理它以及我的代码是否足够。
有任何想法吗?