0
votes

The code below takes values from an NSMutableArray, takes values from an object, then adds them into another NSMutableArray to be used. What I need to do is add a new row for each item in the NSMutableArray *options but it crashes saying:

[__NSSingleObjectArrayI length]: unrecognized selector sent to instance

If I comment out the code in the for (int i = 0; i < options.count; i++) and just leave the NSLog(@"FIELD) it shows the correct values. Any ideas?

LFFormSectionLabel *sectionLabel = [LFFormSectionLabel new];
[sectionLabel addValue:header forSEL:@selector(setText:)];
[vc addSection:sectionLabel];

NSMutableArray *options = [[NSMutableArray alloc] init];
for (NSDictionary *item in self.surveydata) {
    NSString *addressfield = [item objectForKey:@"address_option"];
    [options addObject:addressfield];
}

for (int i = 0; i < options.count; i++) {
    NSString *field = [options objectAtIndex:i];
    LFFormRowTextField *rowTextField = [LFFormRowTextField new];
    rowTextField.key = @"name";
    [rowTextField addValue:field forSEL:@selector(setPlaceholder:)];
    [sectionLabel addRow:rowTextField];

    NSLog(@"FIELD: %@", field);
}
2
You need to provide more details about what's in self.surveydata. But it appears that [item objectForKey:@"address_option"] returns an NSArray, not an NSString. - rmaddy

2 Answers

0
votes

Most likely, the objects in your array aren’t actually NSStrings at all, they’re NSArrays containing an NSString. Try stopping in the debugger after the objectAtIndex call and printing [field class]

0
votes

This is not really an answer but just to help you debug. NSLog is great to help debug code. Add the following NSLog at those lines and see for yourself their contents.

NSMutableArray *options = [[NSMutableArray alloc] init];
for (NSDictionary *item in self.surveydata) {
    NSString *addressfield = [item objectForKey:@"address_option"];
    NSLog("addressfield :%@",addressfield); // HERE
    [options addObject:addressfield];
}
NSLog("options:%@",options);// HERE

for (int i = 0; i < options.count; i++) {
    NSString *field = [options objectAtIndex:i];
    LFFormRowTextField *rowTextField = [LFFormRowTextField new];
    rowTextField.key = @"name";
    [rowTextField addValue:field forSEL:@selector(setPlaceholder:)];
    [sectionLabel addRow:rowTextField];

    NSLog(@"FIELD: %@", field);
}

Your code has a few pitfalls. For example, what if addressfield is not returning an NSString, instead it returns NULL or NSNumber or others? Good practice is to always check the TYPE of object returned from objectForKey before asigning it. Unless you can guarantee that the data contains only string. If it is from a 3rd party Web API, then i can almost say all sorts of rubbish is going to be sent. :D Good luck.