简体   繁体   English

NSData为空问题

[英]NSData is null issues

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData * data = [NSData dataWithContentsOfURL:url];

NSDictionary *regsiter=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSLog(@"response : %@",[regsiter valueForKeyPath:@"response"]);

This NSData values is getting null. 此NSData值变为空。 And i need to upload image also same this link how its possible please help me.... 我也需要上传图片,也与此链接相同,如何可能请帮助我...。

If data is null, then it means it could not be created. 如果数据为空,则意味着无法创建。 But if you want to know the reason, you should use dataWithContentsOfURL:options:error: method. 但是,如果您想知道原因,则应该使用dataWithContentsOfURL:options:error:方法。

Example: 例:

NSError* error = nil;
NSData * data = [NSData dataWithContentsOfURL:url options:nil error:&error];
if (error) {
    NSLog(@"%@", [error localizedDescription]);
}

You can fetch data from URL asynchronously 您可以从URL异步获取数据

NSString *url = [NSString stringWithFormat:item.image];
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

[NSURLConnection sendAsynchronousRequest:req
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *responce,
                                           NSData *data,
                                           NSError *error) {

                           NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                           NSLog(@"String from URL %@ is %@", url, str);

                       }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM