Matheus Rohwedder
Matheus Rohwedder

Reputation: 13

unrecognized selector sent to instance - can someone Help me?

Well I'm trying to set the label text with a Json response if u look at the code if I use @tituloReceita to put it on label it works but i have to use @ingredientes if u take a look here http://blessing.com.br/aplicativos/receitasJson.php, it possible to see that the @ingredientes its a little bigger than @tituloReceita.

code:

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://blessing.com.br/aplicativos/receitasJson.php"]];
    NSError *error=nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *json = dict[@"receitas"];


for (NSDictionary *dic in json) {
    if ([dic[@"tituloReceita"] isEqualToString:titulo]) {

        self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(25, 50, 150, 20)];
        //set the label text
        self.label1.numberOfLines = 10;
        self.label1.text = [dic objectForKey:@"ingredientes"];
        //set the lable font
        self.label1.font = [UIFont boldSystemFontOfSize:10.0f];
        //se the text alignment
        self.label1.textAlignment =  NSTextAlignmentCenter;
        [sView addSubview:self.label1];


     }

}

after this, the app crash and log this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0x10c01ad90'

But if I use the other that a said the @tituloReceita it works perfectly, this app is like a recipe book.

Upvotes: 0

Views: 96

Answers (2)

sschunara
sschunara

Reputation: 2285

From json at http://blessing.com.br/aplicativos/receitasJson.php. "ingredientes" key contains an array and you are assigning it to text take takes string value only. So join object of array and make string. Like for e.g. [[dic objectForKey:@"ingredientes"] componentsJoinedByString:@","];

Upvotes: 0

Yun CHEN
Yun CHEN

Reputation: 6648

According to the JSON, the value of key ingredientes is an Array, you cannot use it as String:

enter image description here

If you want to use all ingredientes, try:

NSArray *ingredientes = [dic objectForKey:@"ingredientes"];
if (ingredientes != nil) {
    self.label1.text = [ingredientes componentsJoinedByString:@","];
}

If you want to use first ingredient, try:

NSArray *ingredientes = [dic objectForKey:@"ingredientes"];
if (ingredientes != nil && ingredientes.count > 0) {
    self.label1.text = [ingredientes firstObject];
}

The last problem is: you create labels with same frame in a loop, that makes all labels together. Maybe you should change frame for each label to place them at good positions.

Upvotes: 4

Related Questions