0

我在 ios 中构建了一个翻译应用程序。该应用程序使用 Yandex 翻译 api。我遵循了本教程:http : //www.raywenderlich.com/5492/working-with-json-in-ios-5 我的 ViewController.m 看起来像这样(我拿出了我的 api 密钥):

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1

#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] ];
    NSData*data = [NSURL URLWithString: [NSString stringWithFormat: @"https://translate.yandex.net/api/v1.5/tr.json/translate?key=apikeys&lang=en-es&text=%@", textfield.text]];
    [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];
  NSString* ttext = [TranslatedText objectAtIndex:0];


// 3) Set the label appropriately
humanReadble.text = [NSString stringWithFormat:@"%@",
                     //[ttext objectForKey:@"name"],
                     ttext];

}

@end`

当我运行应用程序时,我收到错误Thread 1: signal SIGABRT on this line code: return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 我应该怎么办?

4

1 回答 1

1

您的代码中的错误是那里使用了冒号。你应该有这条​​线...

[NSURL URLWithString: [NSString stringWithFormat: @"https://translate.yandex.net/api/v1.5/tr.json/translate?apikeyes&text=%@", textfield.text];

另外,我不知道你为什么要做一个#define. 在处理按钮被按下的方法中获取信息。

NSURL * translateURL = [NSURL URLWithString: [NSString stringWithFormat: @"https://translate.yandex.net/api/v1.5/tr.json/translate?apikeyes&text=%@", textfield.text];
于 2013-09-14T17:45:27.560 回答