Ios 无法识别的选择器已发送到实例-有人可以帮助我吗?
嗯,我试图用Json响应设置标签文本,如果你看代码,如果我使用@Ios 无法识别的选择器已发送到实例-有人可以帮助我吗?,ios,objective-c,iphone,Ios,Objective C,Iphone,嗯,我试图用Json响应设置标签文本,如果你看代码,如果我使用@titureRecita将其放在标签上,它可以工作,但我必须使用@ingredientes如果你看这里,可能会看到@ingredientes比@titureRecita大一点 代码: NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://blessing.com.br/aplicativos/receitasJson.php"]];
titureRecita
将其放在标签上,它可以工作,但我必须使用@ingredientes
如果你看这里,可能会看到@ingredientes
比@titureRecita
大一点
代码:
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];
}
}
在此之后,应用程序崩溃并记录以下内容:
***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\u nsFarray长度]:
已将无法识别的选择器发送到实例0x10c01ad90'
但是如果我使用另一个,即a所说的@
titureRecita
它工作得很好,这个应用程序就像一本食谱。根据JSON,keyingredientes
的值是一个数组,不能将其用作字符串:
如果要使用所有IngRedients,请尝试:
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];
}
最后一个问题是:在循环中创建具有相同帧的标签,这将使所有标签都在一起。也许您应该更改每个标签的框架以将其放置在正确的位置。根据JSON,key
ingredientes
的值是一个数组,不能将其用作字符串:
如果要使用所有IngRedients,请尝试:
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];
}
最后一个问题是:在循环中创建具有相同帧的标签,这将使所有标签都在一起。也许您应该更改每个标签的框架以将它们放置在正确的位置。来自json at。“ingredientes”键包含一个数组,您正在将其分配给文本take takes string value only。所以连接数组的对象并生成字符串。例如,
[[dic objectForKey:@“ingredientes”]组件通过字符串连接:@“,”代码>来自json。“ingredientes”键包含一个数组,您正在将其分配给文本take takes string value only。所以连接数组的对象并生成字符串。例如,[[dic objectForKey:@“ingredientes”]组件通过字符串连接:@“,”代码>请仔细检查您的JSONtitureRecita
是一个字符串,ingredientes
是一个数组。[dic objectForKey:@“ingredientes”]代码>它是一个数组而不是一个string@SushilSharma对不起,我是iOS和objective-c的新手,无法识别的选择器(可能不是直接调用您,而是“隐藏”调用)length
methdo(在NSArray
对象上)(您不匹配对象的实际类,并认为它可以调用length
,但事实并非如此,因为它是NSArray
对象)。通过实践,我们可以说,在某个地方,您将NSArray
对象与NSString
对象不匹配。精确定位准确的行应该给您self.label1.text=[dic objectForKey:@“ingredientes”];
。[dic objectForKey:@“ingredientes”];
是一个NSArray
,而不是NSString
,您的解析是错误的。请仔细检查您的JSON。Titolorecita
是一个字符串,IngRedients
是一个数组。[dic objectForKey:@“IngRedients”];
它是一个数组而不是一个string@SushilSharma对不起,我是iOS和objective-c的新手,无法识别的选择器(可能不是直接调用您,而是“隐藏”调用)length
methdo(在NSArray
对象上)(您不匹配对象的实际类,并认为它可以调用length
,但事实并非如此,因为它是NSArray
对象)。通过实践,我们可以说,在某个地方,您将NSArray
对象与NSString
对象不匹配。精确定位准确的行应该给您self.label1.text=[dic objectForKey:@“ingredientes”];
.So[dic objectForKey:@“ingredientes”];
是一个NSArray
,而不是NSString
,您的解析是错误的。那么,我如何将其放在uilabel上呢?@YunCHEN回答得很好,您知道如何更改“,”以使换行更清晰、更简单?@MatheusRohwedder,试试“\n”。请记住将label的numberOfLines属性设置为0。那么,如何将其放在uilabel上?@YunCHEN回答得好,你知道如何更改“,”以使其更清晰、更容易?@MatheusRohwedder,请尝试@“\n”。请记住将label的numberOfLines属性设置为0。回答得好,但你知道如何更改“,”要使应用程序清晰,请使用换行符?是,而不是,请使用\n换行符。但您需要将label.numberOfLines=0使其成为多行。Json代码我无法更改,因此如何以编程方式替换它?将[[dic objectForKey:@“IngRedients”]组件替换为[[dic objectForKey:@“IngRedients”];替换为[[dic objectForKey:@“IngRedients”]componentsJoinedByString:@“\n”];对不起,我理解错了,我想你说过要更改Json,非常感谢。回答得好,但你知道我如何更改“,”要使应用程序清晰,请使用换行符?是,而不是,请使用\n换行符。但您需要将label.numberOfLines=0使其成为多行。Json代码我无法更改,因此如何以编程方式替换它?将[[dic objectForKey:@“IngRedients”]组件替换为[[dic objectForKey:@“IngRedients”];替换为[[dic objectForKey:@“IngRedients”]componentsJoinedByString:@“\n”];对不起,我理解错了,我想你说过要更改Json,非常感谢。