ios - Get specific objects from xml document -


i'm trying create array, xml documents. problem want objects id equal passedid. id previous viewcontroller. how can implement this?

i've created gives me phrase objects.

- (void)viewdidload {     [super viewdidload];     nsstring* path = [[nsbundle mainbundle] pathforresource: @"lists" oftype: @"xml"];     nsdata* data = [nsdata datawithcontentsoffile: path];      rows = [[nsmutablearray alloc] init];        nsxmlparser* parser = [[nsxmlparser alloc] initwithdata: data];      [parser setdelegate:self];     [parser parse];   // additional setup after loading view. }   - (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qualifiedname attributes:(nsdictionary *)attributedict {     if ([elementname isequaltostring:@"phrase"]) {         translation = attributedict[@"translation"];         meaning = attributedict[@"meaning"];         pronounce = attributedict[@"pronounce"];         theid = attributedict[@"id"];          nsarray *cols = @[translation, meaning, pronounce, theid ];          [rows addobject: cols];        }  } 

your rows array contains arrays of data. rather inefficient , not maintainable. better served using array of dictionaries or custom objects. mean accessing data key / name more obvious , easier. it's more accurate.

currently, can use predicate check if desired id in row array. collisions other objects.

if change dictionary or custom object (you should) can use key explicitly in predicate:

nspredicate *filterpredicate = [nspredicate predicatewithformat:@"theid == %@", id]; nsarray *requiredrows = [rows filteredarrayusingpredicate:filterpredicate]; 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -