【问题标题】:How to make a json call with objective C [closed]如何使用目标 C 进行 json 调用 [关闭]
【发布时间】:2019-12-24 04:18:20
【问题描述】:

我正在使用 json 快速实现调用,但我需要使用 Objective C 进行调用。谁能帮我将此代码转换为Objective C。它属于可以转换货币的项目。我真的很感激任何帮助。我的问题是如何使用目标 C 进行 json 调用。我尝试了一些快速到客观的 C 转换器,但它们都不能正常工作。这就是为什么我在这里问我的问题,

   let url = URL(string: "https://something")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in            
        if (error != nil)
        {
            print("ERROR")
        }
        else
        {
            if let content = data
            {
                do
                {
                    let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    if let rates = myJson["rates"] as? NSDictionary
                    {
                        for (key, value) in rates
                        {
                            self.myCurrency.append((key as! String))
                            self.myValues.append((value as? Double)!)                              
                        }
                    }
                }
                catch
                {
                }                    
            }                
        }
        self.pickerView.reloadAllComponents()
        //myPicker.selectRow(row, inComponent: 0, animated: true)
        self.pickerView.selectRow(20, inComponent: 0, animated: false)
    }
    task.resume()        
}

【问题讨论】:

  • 这不是免费的代码编写服务,您应该自己努力编写一些我们可以帮助您的代码。

标签: ios objective-c json swift xcode


【解决方案1】:

我可能会建议您查看以前的一些类似帖子并使用它们来指导您找到答案。 Objective-C 在很大程度上遵循与您在 Swift 示例中使用的模式相同的模式。

我尚未对此进行测试,但您的代码最终会如下所示。

NSURL *url = [NSURL URLWithString:@"https:/something"];
NSURLSessionDataTask * task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if ((error) || (!data)) {
        NSLog(@"Error");
    } else {
        NSError *err = nil;
        NSDictionary *json;
        json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &err];
        if (json != nil && err == nil) {
            [json enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                [myCurrency addObject:key];
                [myValues addObject:obj];
            }];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self.pickerView reloadAllComponents];
                [self.pickerView selectRow:20 inComponent:0 animated:false];

           });
        }
    }
}];
[task resume];

【讨论】:

  • 非常感谢。在了解 Objective C 的外观时,您为我提供了很多帮助。非常感谢
猜你喜欢
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多