我是新来的iOS开发和遇到问题比较两个NSMutableArray
。如何比较两个NSMutableArrays?
其中之一(在appDelegate.array
代码)包含数据从数据库和另一个(dataArray
在代码)来包含数据从一个服务器响应到来。
现在我想的(dataArray
)的每个元素与全(appDelegate.array
)比较和如果(dataArray
)的一些元素(appDelegate.array
)存在,则什么也不做或断裂,如果不存在,然后将其添加到数据库中。
我试过,但我无法做到这一点。以下是我正在使用的代码。
预先感谢您。
NSMutableArray *dataArray = [responseDictionary objectForKey:@"myDATA"];
NSLog(@"%d dataArray count", [dataArray count]);
for (int i = 0; i < [dataArray count]; i++)
{
NSLog(@"%d delegate array count",[appDelegate.array count]);
NSInteger ID = [[[dataArray objectAtIndex:i] objectForKey:@"ID"] intValue];
NSString *Note = [[dataArray objectAtIndex:i] objectForKey:@"Note"];
NSString *Reminder = [[dataArray objectAtIndex:i] objectForKey:@"Reminder"];
NSInteger Status = [[[dataArray objectAtIndex:i] objectForKey:@"Completed"]intValue];
NSInteger DisplayOrder = [[[dataArray objectAtIndex:i] objectForKey:@"Display_Order"] intValue];
if ([appDelegate.array count] == 0 && Note != nil)
{
NSString *query = [NSString stringWithFormat:@"INSERT INTO Tasks (Note, Reminder, Status, DisplayOrder) VALUES ('%@','%@','%d','%d')",Note, Reminder, Status, DisplayOrder];
//NSLog(@"%@",query);
sqlite3 *database_1;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *database_Path = [documentsDir stringByAppendingPathComponent:@"TODO.sqlite"];
if(sqlite3_open([database_Path UTF8String], &database_1) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement_1;
if(sqlite3_prepare_v2(database_1,[query UTF8String], -1, &compiledStatement_1, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement_1) == SQLITE_ROW)
{
sqlite3_step(compiledStatement_1);
}
}
sqlite3_finalize(compiledStatement_1);
}
sqlite3_close(database_1);
[self readDataFromDatabase];
}
else
{
// NSLog(@"Entered Else");
for (int k = 0; k < [appDelegate.array count]; k++)
{
objectData = [appDelegate.array objectAtIndex:k];
// NSLog(@"%d",objectData.ID);
NSMutableArray *IDArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.ID]stringValue]];
NSMutableArray *NoteArray = [NSMutableArray arrayWithObject:objectData.Note];
NSMutableArray *ReminderArray = [NSMutableArray arrayWithObject:objectData.Reminder];
NSMutableArray *StatusArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.Status]stringValue]];
NSMutableArray *DisplayOrderArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.DisplayOrder]stringValue]];
NSLog(@"%@ server",[NSString stringWithFormat:@"%d",ID]);
NSLog(@"%@ database",IDArray);
if ([CommonFunctions isValueInArray:[NSString stringWithFormat:@"%d",ID]:IDArray] && [CommonFunctions isValueInArray:Note :NoteArray] && [CommonFunctions isValueInArray:Reminder :ReminderArray] && [CommonFunctions isValueInArray:[NSString stringWithFormat:@"%d",Status] :StatusArray] && [CommonFunctions isValueInArray:[NSString stringWithFormat:@"%d",DisplayOrder] :DisplayOrderArray])
{
NSLog(@"Present In appDelegate.array!!!");
}
else
{
NSString *query = [NSString stringWithFormat:@"INSERT INTO Tasks (Note, Reminder, Status, DisplayOrder) VALUES ('%@','%@','%d','%d')",Note, Reminder, Status, DisplayOrder];
NSLog(@"%@",query);
sqlite3 *database_1;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *database_Path = [documentsDir stringByAppendingPathComponent:@"TODO.sqlite"];
if(sqlite3_open([database_Path UTF8String], &database_1) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement_1;
if(sqlite3_prepare_v2(database_1,[query UTF8String], -1, &compiledStatement_1, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement_1) == SQLITE_ROW)
{
sqlite3_step(compiledStatement_1);
}
}
sqlite3_finalize(compiledStatement_1);
}
sqlite3_close(database_1);
}
}
}
看到http://stackoverflow.com/questions/6137469/how-to-compare-two-nsmutablearray –
你的代码似乎是正确的。我可能会犯一些小错误。你能告诉我们究竟发生了什么问题吗? – Rushi
ID是唯一的吗?如果是这样,你可以在appDelegate.array上做一个谓词来查找所有不在其他数组中的元素。也就是说,“获取ID不存在于数组中的appDelegate.array中的所有元素”。看看NSPredicate,也许是块基的方法。现在,迭代已过滤的数组并存储该数组中的所有元素。 – simonbs