【问题标题】:compare all the objects of one nsmutable array with all the objects of another nsmutable array and get new objects into another nsarray将一个 nsmutablearray 的所有对象与另一个 nsmutablearray 的所有对象进行比较,并将新对象放入另一个 nsarray
【发布时间】:2012-11-01 07:11:10
【问题描述】:
  • 我正在尝试将所有联系人备份到服务器上。

  • 所以我将联系人转换为 vCard 字符串并发送
    vCard 字符串到服务器上,并存储 vCard 字符串 RecordID 到 sqlite 数据库中

  • 所以当用户第一次想要收回所有的 联系人将转到服务器和数据库中。

  • 从第二次开始,我只想将新联系人发送到 服务器。所以我需要比较存储的数据库RecordIds 使用 Phonebook RecordIds 进入 nsarray 并获取新的 RecordIds 到一个数组中

  • 例如,我有像 40,41,42,43,44,45 这样的 RecordIds 到 dbarray.不一定按顺序或顺序。

  • 现在当用户添加 5 个新联系人时,电话簿数组将包含 recordIds 像 40,41,42,43,44,45,46,47,48,50

  • 所以这次我只想用recordIds发送联系人 46、47、48、49 和 50。类似于增量备份

  • 我还需要检查是否有任何修改 存储的 vCards.So 我还需要比较所有 vCard 字符串 电话簿数组和数据库数组。

    我是 iOS 编程新手,我被困住了。请 帮助!!!很紧急

【问题讨论】:

    标签: ios xcode nsstring nsmutablearray compare


    【解决方案1】:

    我通过使用以下代码解决了上述问题。希望它对某人有所帮助。

     -(void)newContactsAndContactsToUpdate
      {
        NSMutableArray *newContact = [[NSMutableArray alloc]init];
        NSMutableArray *newContactRecIds = [[NSMutableArray alloc]init];
        NSMutableArray *updateContactRecIds = [[NSMutableArray alloc]init];
        NSMutableArray *updateContactRecIds = [[NSMutableArray alloc]init];
        //dataArray contains all the contacts(converted into Vcard strings) in the phonebook
        //recordIDArray contains the all recordids in the phonebook
        //dbArraywithRID contains the recordids stored in database.
        //dbArraywithVcard contains  contacts(converted into Vcard strings)stored in database
        for(int i=0;i< dataArray.count;i++)
        {
        BOOL found = NO;
        int phoneBookRecId=[[recordIDArray objectAtIndex:i] intValue];
    
    
        NSString * currentPhVc=[dataArray objectAtIndex:i];
    
        for(int j=0;j< dbArraywithRID.count;j++)
        {
            int dbRecId=[[dbArraywithRID objectAtIndex:j] intValue];
    
            if(dbRecId == phoneBookRecId)
            {
                found =YES;
    
                NSString * currentDBVc=[dbArraywithVcard objectAtIndex:j];
    
                if(![currentDBVc isEqualToString:currentPhVc])
                {
                    NSLog(@" db rec  =%@    -   %@   ",currentDBVc,currentPhVc );
                    [updateContactRecIds addObject:[NSNumber numberWithInt:phoneBookRecId]];
    
                    [newContact addObject:currentPhVc];
                    [newContactRecIds addObject:[NSNumber numberWithInt:phoneBookRecId]];
    
                }
                break;
            }
        }
        if(!found)
        {
            [newContact addObject:currentPhVc];
            [newContactRecIds addObject:[NSNumber numberWithInt:phoneBookRecId]];
    
        }
    }
    
    
    if(newContact!=nil && [newContact count] > 0)
    {
        //newContact array contains all the contacts(vcard strings) that are new
    
    }
    if(updateContactRecIds!=nil && [updateContactRecIds count] > 0)
    {
        updateContactRecIds contains all the recordids which need to be updated
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2023-03-12
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      相关资源
      最近更新 更多