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 code line: 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 に答える