2016-08-29 3 views
0

Hallo Ich habe einen NSMutableDictionary wie dieseWie wird ein Wert für einen bestimmten Schlüssel in NSMutableArray von NSMutableDictionaries gesucht?

<__NSArrayM 0x14e226ff0>(
{ 

DocumentName = "IMG_2597.JPG"; 
DocumentType = JPG; 
Image = "<UIImage: 0x14e52c370> size {1239, 1242} orientation 0 scale 1.000000"; 

} 

ich mehrere Objekte in meine NSMutablearray bin hinzufügen. Aber ich möchte prüfen, ob dieses Bild bereits in der NSMutablearray verfügbar ist. Eigentlich plane ich, es durch die DocumentName zu suchen. Wie kann ich überprüfen, ob derselbe Wert für den Schlüssel DocumentName im Array bereits vorhanden ist? Ich möchte das NSMutableDictionary hinzufügen, wenn es nur nicht bereits existiert. So erstelle ich mein NSMutabledictionary Objekt.

NSMutableDictionary *imageDictionary=[[NSMutableDictionary alloc] init]; 
[imageDictionary setObject:chosenImage forKey:@"Image"]; 
[imageDictionary setValue:strDocNAme forKey:@"DocumentName"]; 
[imageDictionary setValue:strExtension forKey:@"DocumentType"]; 
[mutarrayImages addObject:imageDictionary]; 

Bitte helfen Sie mir. Dank

Antwort

2

Sie können versuchen, eine dieser zu prüfen, ob das Bild bereits existiert oder nicht.

1) Unter Verwendung Prädikat

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DocumentName = 'IMG_2597.JPG'"]; 
NSArray *arr = [yourArray filteredArrayUsingPredicate:predicate]; 
if (arr.count > 0) { 
    NSLog(@"Object is contain"); 
} 

2) Unter Verwendung ValueForKey

NSArray *documentName = [yourArray valueForKey:@"DocumentName"]; 
if ([documentName containsObject:@"IMG_2597.JPG"]) { 
    NSLog(@"Object is contain"); 
} 
+0

meine Array kann mehrere NSDictionaries haben. Es ist eine Reihe von Wörterbüchern. Also kann ich direkt zugreifen wie [arr enthältObkekt: @ "IMG_2597.JPG"] keine Notwendigkeit, den Indexpfad zur Verfügung zu stellen? – Irrd

+0

Danke. Es funktionierte :) – Irrd

0
NSArray *allObjectsArray; // your array 

NSMutableArray *resultObjectsArray = [NSMutableArray array]; 
for(NSDictionary *dict in allObjectsArray) 
{ 
    NSString *docName = [dict objectForKey:@"DocumentName"]; 
    NSRange range = [docName rangeOfString:@"your string which you want to match" options:NSCaseInsensitiveSearch]; 
    if(range.location != NSNotFound) 
     [resultObjectsArray addObject:dict]; 
} 
1
NSSet* myValuesSet = [NSSet setWithArray: [mutarrayImages valueForKey:@"DocumentName"]]; 
if ([[myValuesSet allObjects] containsObject:< DocumentName to compare >]) { 
    NSLog(@"Exist"); 
} 
else{ 
    NSLog(@"Does not exist"); 
} 
Verwandte Themen