【问题标题】:How can i check the value from NSDictionary and integer values are same?如何检查 NSDictionary 中的值和整数值是否相同?
【发布时间】:2013-03-25 10:02:03
【问题描述】:

我有一个字典,其中的值如图所示,我有一个整数值的 book id 。如何检查 bookid 是否匹配?

这是我的检查代码

NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:metaDataPath];
NSMutableArray *notes=[plistdictionary objectForKey:@"usernotes"];
NSLog(@"notes value %@",notes);
NSArray *CollectingBookid=[[NSArray alloc]init];
CollectingBookid=[notes valueForKey:@"bookid"];
NSArray *CollectingPages=[[NSArray alloc]init];
CollectingPages=[notes valueForKey:@"pagenumber"];
NSArray *CollectingNotes=[[NSArray alloc]init];
CollectingNotes=[notes valueForKey:@"notes"];
NSLog(@"collection of book id%@",CollectingBookid);
NSString *bookid=@"95";
NSString *page=@"1";
int c=[CollectingBookid count];
for(int i=0;i<c;i++)
{
    NSString *singleBookids=[CollectingBookid objectAtIndex:i];
    NSString *singlePage=[CollectingPages objectAtIndex:i];
    if([singleBookids isEqualToString:bookid])
        {
            if([singlePage isEqualToString:page])
            {
                NSMutableArray *CompleteUserNotes=[[NSMutableArray alloc]init];
                CompleteUserNotes=[CollectingNotes objectAtIndex:i];
                NSLog(@"Selected Notes%@",CompleteUserNotes);
            }
        }
}

【问题讨论】:

  • 为什么 bookid 是 pList 中的字符串?您想将整数与整数进行比较吗?或者你想比较字符串和字符串?
  • 如何在字典中添加整数值?
  • 这些与您的图片不匹配: NSArray *CollectingBookid=[[NSArray alloc]init]; CollectingBookid=[notes valueForKey:@"bookid"]; NSArray *CollectingPages=[[NSArray alloc]init]; CollectingPages=[notes valueForKey:@"pagenumber"]; NSArray *CollectingNotes=[[NSArray alloc]init]; CollectingNotes=[notes valueForKey:@"notes"];

标签: ios xcode nsarray plist nsdictionary


【解决方案1】:

创建谓词并通过过滤数组找到userNote

/*As your plist has bookId as string its taken as string. 
But if you have bookId as integer before checking 
convert it to string for predicate to work*/
NSInteger bookId = 92;
NSString *bookIdString = [NSString stringWithFormat:@"%d",bookId];

NSInteger pageNumber = 12;
NSString *pageNumberString = [NSString stringWithFormat:@"%d",pageNumber];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bookid == %@ AND pagenumber == %@",bookIdString,pageNumberString];

//filteredUserNotes will have all notes matching bookId
NSArray *filteredUserNotes =  [notes filteredArrayUsingPredicate:predicate];

//Assuming you only has one entry for bookId and you want a single one
NSDictionary *userNote = [filteredUserNotes lastObject];

【讨论】:

  • @Naveen 你想找到一个与 bookId rite 匹配的 userNote 项目。在此代码中,您创建一个谓词,它将找到 bookId 与您在谓词中提供的匹配的笔记。在过滤结果是数组时,然后您获取该结果并执行您想要的操作
  • 但我想要页码和 bookid 匹配!!
  • @Naveen 我已经编辑了代码。如果有任何问题,请告诉我。
  • 如果我给出我的输入而不是你的硬编码 bookid 和页码,你说我不需要检查所有条件而是我可以使用谓词 thaere?
  • @Naveen 是的,你是对的。我硬编码这些值只是为了向您展示。
【解决方案2】:

您使用字符串格式进行正确比较。

如果您想与整数进行比较,请使用以下代码:

int bookid=95;
int page=1;
int c=[CollectingBookid count];
for(int i=0;i<c;i++)
{
    NSString *singleBookids=[CollectingBookid objectAtIndex:i];
    NSString *singlePage=[CollectingPages objectAtIndex:i];
    if([singleBookids intValue]==bookid)
    { 
        if([singlePage intValue]==page)
        {
            NSMutableArray *CompleteUserNotes=[[NSMutableArray alloc]init];
            CompleteUserNotes=[CollectingNotes objectAtIndex:i];
            NSLog(@"Selected Notes%@",CompleteUserNotes);
        }
    }
}

【讨论】:

    【解决方案3】:

    我在记事本里写这个。对可能的错误深表歉意。

     NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:metaDataPath];
    NSMutableArray *notes=[plistdictionary objectForKey:@"usernotes"];
    int bookid = 95;
    int page = 1;
    for (NSDictionary *collectingObject in notes)
    {
        int collectingBookid = [[collectingObject objectForKey:@"bookid"] intValue];
        int collectingPageid = [[collectingObject objectForKey:@"pagenumber"] intValue];
        if (collectingBookid == bookid && collectingPageid == page)
        {
            NSString *collectingNote = [collectingObject objectForKey:@"notes"];
            NSLog(@"Note is: %@", collectingNote);
        }
    }
    

    【讨论】:

    • 如何将整数转换为字符串?
    • int number = 100500; NSString *stringFromInteger = [NSString stringWithFormat:@"%d", number];
    【解决方案4】:

    你可以用它来比较整数值

    [singleBookids intValue]==[bookid intValue];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2015-10-16
      • 2015-08-23
      相关资源
      最近更新 更多