将数据从Json提取到标签文本IOS中
因此,我使用pokedex API作为IOS和web服务的学习曲线 这是连接完成时我的DidReceiveData将数据从Json提取到标签文本IOS中,ios,objective-c,Ios,Objective C,因此,我使用pokedex API作为IOS和web服务的学习曲线 这是连接完成时我的DidReceiveData - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ //If the resposne recieved is good the call this function // NSLog(@"data is %@", data); //NSString
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//If the resposne recieved is good the call this function
// NSLog(@"data is %@", data);
//NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"string is %@", myString);
//Put data into a string
NSError *e = nil;
pokeDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
NSLog(@"dictionary is %@", pokeDictionary);
}
这将Json输出到控制台,我可以像这样将其登录到控制台
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded!");
NSLog(@"The Pokemon's name is %@", pokeDictionary[@"name"]);
NSLog(@"The Pokemon's attack is %@", pokeDictionary[@"attack"]);
NSLog(@"The Pokemon's speed is %@", pokeDictionary[@"speed"]);
}
{
self.pokemonAttack.text = (@"The Pokemon's speed is %@", pokeDictionary[@"name"]);
self.pokemonAttack.text = (@"The Pokemon's speed is %@", pokeDictionary[@"attack"]);
self.pokemonSpeed.text = (@"The Pokemon's speed is %@", pokeDictionary[@"speed"]);
}
但是,我们尝试将Json提取到如下文本字段中
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded!");
NSLog(@"The Pokemon's name is %@", pokeDictionary[@"name"]);
NSLog(@"The Pokemon's attack is %@", pokeDictionary[@"attack"]);
NSLog(@"The Pokemon's speed is %@", pokeDictionary[@"speed"]);
}
{
self.pokemonAttack.text = (@"The Pokemon's speed is %@", pokeDictionary[@"name"]);
self.pokemonAttack.text = (@"The Pokemon's speed is %@", pokeDictionary[@"attack"]);
self.pokemonSpeed.text = (@"The Pokemon's speed is %@", pokeDictionary[@"speed"]);
}
错误是“表达式结果未使用”,我想我的主要问题是我对objective-c不太适应,只是在IOS中乱闯。为此,我表示歉意,并理解“做目标c课程”的评论
如果你能给我指出正确的方向,我可以继续我的火试,我想我也应该很快转向swift。首先,你应该知道,当连接以增量方式加载数据时,委托方法
connection:didReceiveData:
可能会被多次调用。如果返回的数据非常短,则可能会调用一次,但很可能会在某个时候中断,因为您最终将尝试解析不完整的数据
关于你得到的警告-你不能像那样格式化字符串。您试图像调用NSLog一样使用字符串格式,但NSLog方法会为您进行格式设置。您需要这样做:
self.pokemonAttack.text = [NSString stringWithFormat:@"The Pokemon's speed is %@", pokeDictionary[@"speed"]];
看一看。这将指导您正确连接字符串。感谢您的指针:)它确实返回(null),但至少它将我置于正确的路径返回null,因为在错误的位置调用了null,这是我的错