Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching for objects in array by name
    primarykey
    data
    text
    <p>is there a possibility to check for an object in an array by name. At some point in my app I need to access the plist and pull information stored under an object which I found by its name. The reason I need to do this is because I don't have the integer number under which where the object is placed in my array.</p> <p>I have started the usual procedure but i'm not getting anywhere. Maybe its just getting a little too late for me....</p> <p>This is how my plist looks</p> <pre><code>Array Dictionary title ... text ... ect ... Dictionary title ... text ... ect ... Dictionary title ... text ... ect ... </code></pre> <p>My code so far isn't helping at all. I'm definitely doing something wrong.</p> <pre><code>-(NSString*)nameFromPlist:(NSString*)name { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Orte.plist"]; _regionArray = [NSArray arrayWithContentsOfFile:writableDBPath]; NSString *string = [[NSString alloc]init]; for(NSDictionary *dict in _regionArray) { string = [[dict objectForKey:name] valueForKey:@"title"]; } return [NSString stringWithFormat:@"%@",string]; </code></pre> <p>}</p> <p>My string is just returning null.</p> <p>Any ideas anyone? Thanks a lot!</p> <p>ANSWER:</p> <p>Here is the code for everyone:</p> <p>-(NSString*)nameFromPlist:(NSString*)name {</p> <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *tmpFileName = [[NSString alloc] initWithFormat:@"Orte.plist"]; NSString *path = [documentsDirectory stringByAppendingPathComponent:tmpFileName]; NSString *string; NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path]; NSDictionary *dict = [[NSDictionary alloc] init]; NSString *title; for (int i=0; i&lt;[array count]; i++) { dict = [array objectAtIndex:i]; title = [dict valueForKey:@"title"]; if([title isEqualToString:name]) { string = [dict valueForKey:@"title"]; NSLog(@"Titel: %@", string); } } return [NSString stringWithFormat:@"%@",string]; </code></pre> <p>}</p> <p>Thank a lot everyone!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload