我正在使用 Yandex API 开发一个 ios 翻译应用程序。我遵循了本教程:教程
我的 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
我究竟做错了什么?