我正在使用Yandex API开发ios翻译应用程序。我遵循了本教程:Tutorial
我的ViewController.m文件看起来像这样(我拿出了我的api密钥):
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define TranslateText [NSURL URLWithString: @"https://translate.yandex.net/api/v1.5/tr.json/translate?key=APIKEY&lang=en-es&text=To+be,+or+not+to+be%3F"] //2
#import "ViewController.h"
@end
@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
-(NSData*)toJSON
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: TranslateText];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* TranslatedText = [json objectForKey:@"text"]; //2
NSLog(@"Text that was translated: %@", TranslatedText); //3
// 1) Get the latest loan
NSDictionary* ttext = [TranslatedText objectAtIndex:0];
// 3) Set the label appropriately
humanReadble.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another",
[ttext objectForKey:@"name"],
[(NSDictionary*)[ttext objectForKey:@"location"] objectForKey:@"country"]];
}
@end
当我运行我的应用程序(iphone 6.1模拟器)时,它会给我这个错误并且应用程序会暂停。
2013-07-31 09:57:51.111 Translate[76046:c07] Text that was translated: (
"Ser, o no ser?"
)
2013-07-31 09:57:51.146 Translate[76046:c07] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x71d1ef0
2013-07-31 09:57:51.150 Translate[76046:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x71d1ef0'
*** First throw call stack:
(0x1c91012 0x10cee7e 0x1d1c4bd 0x1c80bbc 0x1c8094e 0x2c57 0x10e26b0 0xb0e765 0x1c14f3f 0x1c1496f 0x1c37734 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x12ffc 0x1f5d 0x1e85)
libc++abi.dylib: terminate called throwing an exception
(lldb)
它还会打开一个文件main.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
它指出了这一行:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
并说线程1:信号SIGABRT
我做错了什么?
答案 0 :(得分:0)
从NSLog输出似乎
NSArray* TranslatedText = [json objectForKey:@"text"];
是字符串的数组,而不是字典的数组。所以你应该替换
NSDictionary* ttext = [TranslatedText objectAtIndex:0];
通过
NSString* ttext = [TranslatedText objectAtIndex:0];
并将[ttext objectForKey:@"name"]
替换为ttext
。