好吧,如果我使用@tituloReceita
将其放在标签上,那么我正在尝试使用 Json 响应设置标签文本,但我必须使用 @ingredientes
如果你看这里 http://blessing.com.br/aplicativos/receitasJson.php , 可以看到 @ingredientes
比 @tituloReceita
大一点。
代码:
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];
}
}
在此之后,应用程序崩溃并记录如下:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0x10c01ad90'
但是,如果我使用 @tituloReceita
所说的另一个应用程序,它会完美运行,这个应用程序就像一本食谱书。
最佳答案
根据JSON,key ingredientes
的值是一个Array,你不能把它当作String来使用:
如果您想使用所有成分,请尝试:
NSArray *ingredientes = [dic objectForKey:@"ingredientes"];
if (ingredientes != nil) {
self.label1.text = [ingredientes componentsJoinedByString:@","];
}
如果您想使用第一种成分,请尝试:
NSArray *ingredientes = [dic objectForKey:@"ingredientes"];
if (ingredientes != nil && ingredientes.count > 0) {
self.label1.text = [ingredientes firstObject];
}
最后一个问题是:您在循环中创建具有相同框架的标签,这使得所有标签在一起。也许您应该更改每个标签的框架以将它们放置在合适的位置。
关于ios - 发送到实例的无法识别的选择器 - 有人可以帮助我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44282696/