iphone - UITextField 和 NSURL URLWithString

标签 iphone ios json translate

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

#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: 返回 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 我该怎么办?

最佳答案

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

[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];

关于iphone - UITextField 和 NSURL URLWithString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804558/

相关文章:

iPhone 通过代码将某些内容复制到剪贴板

iphone - 允许多台 Mac 处理同一个 iOS 项目

ios - 检查项目是否已保存到 CoreData

ios - 强制 PDF 页面填充上下文

ios - 自定义 UITableViewCell 中缺少附件

php - 将多个数据输入数据库

iphone - 无法更新数据库

iOS:UITableView reloadData 无法刷新数据

javascript - 最不同的数据交换格式?

json - 如何将 png 文件包含在 JSON 文件的 Swift 对象中?