gpt4 book ai didi

iphone - dispatch_async 没有被调用

转载 作者:行者123 更新时间:2023-11-29 13:17:49 26 4
gpt4 key购买 nike

我在将 JSON 数据存储到单个对象数组中时遇到问题。看起来问题出在处理 JSON 请求的 dispatch_asynch 的执行中。当我在方法之前创建一个断点而不是单步执行应用程序时,它似乎只是通过发送到 dispatch_async 的 block 。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
NSString *json = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
NSLog(@"\nJSON: %@ \n Error: %@", json, error);

if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil];

for (NSString *name in [jsonDict valueForKeyPath:@"name"]) {
NSString *tempString = [[NSString alloc] initWithString:name];
Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray];
[self addCompany:company];

非常感谢大家对这个问题的帮助和支持。

最佳答案

 if(!error) {
...
}
else
{
NSLog(@"%@",[error localizedDescription]);
}

记录错误并找出发生了什么

这段代码我试过并且运行良好

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
NSString *json = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
NSLog(@"\nJSON: %@ \n Error: %@", json, error);

if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil];

for (NSString *name in [jsonDict valueForKeyPath:@"name"]) {
NSString *tempString = [[NSString alloc] initWithString:name];
NSLog(@"%@",tempString);
}
}
});

回应

2013-03-05 22:02:44.312 newTrial[4711:12303] 
JSON: [{"created_at":"2013-03-04T00:09:06Z","id":1,"name":"Apple","updated_at":"2013-03-04T00:09:06Z","actions":[{"created_at":"2013-03-04T00:09:07Z","id":1,"name":"Call Support Desk","updated_at":"2013-03-04T00:09:07Z"},{"created_at":"2013-03-04T00:09:07Z","id":2,"name":"Call Genius Bar","updated_at":"2013-03-04T00:09:07Z"}]},{"created_at":"2013-03-04T02:01:49Z","id":2,"name":"Comcast","updated_at":"2013-03-04T02:01:49Z","actions":[{"created_at":"2013-03-04T02:01:49Z","id":3,"name":"Account Services","updated_at":"2013-03-04T02:01:49Z"}]}]
Error: (null)
2013-03-05 22:02:51.766 newTrial[4711:12303] Apple

所以我假设问题出在你将值存储到数组的位置。检查它是否已正确初始化问题可能出在这里

Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray];
[self addCompany:company];

关于iphone - dispatch_async 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com