-2

写真のような値の辞書があり、本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);
            }
        }
}
4

4 に答える 4

2

述語を作成し、配列をフィルタリングして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];
于 2013-03-25T10:14:11.283 に答える
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);
        }
    }
}
于 2013-03-25T10:24:09.503 に答える
2

私はこれをメモ帳で書いています。エラーの可能性があります。

 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);
    }
}
于 2013-03-25T10:25:54.737 に答える
0

これを使用して整数値を比較できます

[singleBookids intValue]==[bookid intValue];
于 2013-03-25T10:15:32.517 に答える