在 Block 运行之前返回 NSString 值 [英] NSString value returning before Block has run

查看:68
本文介绍了在 Block 运行之前返回 NSString 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在块外的块(fid")内生成的值.问题是该值在块运行之前被拉取,因此即使数据存在,该值仍返回为 (null).有谁知道我怎样才能做到这一点?见代码:

I'm trying to use a value generated inside of a block ("fid") outside of a block. The problem being that the value is being pulled before the block has run, and so the value returns as (null), even though the data is present. Does anyone know how I can make this work? See code:

.h

@property (nonatomic, strong) NSString *fid;

.m

[DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSLog(@"File uploaded!");


  [file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"];
  [file removeObjectForKey:@"file"];

  fid = [responseObject objectForKey:@"fid"];

  NSLog(@"%@",responseObject);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Failed to upload file!");
}];

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject: [NSString stringWithFormat:@"%@", fid] forKey:@"fid"];
NSLog(@"%@", fid);

NSDictionary *fidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:dict] forKey:@"und"];

[nodeData setObject:fidLangDict forKey:@"field_photo"];

推荐答案

移动代码块内的代码

[DIOSFile fileSave:file success:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSLog(@"File uploaded!");


  [file setObject:[responseObject objectForKey:@"fid"] forKey:@"fid"];
  [file removeObjectForKey:@"file"];

  fid = [responseObject objectForKey:@"fid"];

  NSLog(@"%@",responseObject);
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];

  [dict setObject: [NSString stringWithFormat:@"%@", fid] forKey:@"fid"];
  NSLog(@"%@", fid);

  NSDictionary *fidLangDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:dict] forKey:@"und"];

  [nodeData setObject:fidLangDict forKey:@"field_photo"];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Failed to upload file!");
}];

这篇关于在 Block 运行之前返回 NSString 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆