在Block运行之前返回的NSString值

时间:2015-04-28 17:50:37

标签: ios objective-c nsstring

我尝试使用块内部(" fid")内部生成的值。问题是在块运行之前正在拉取值,因此值返回为(null),即使数据存在。有谁知道我怎么能做这个工作?见代码:

·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"];

2 个答案:

答案 0 :(得分:2)

移动块内的代码

[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!");
}];

答案 1 :(得分:1)

有两种方法可以做到这一点:

  1. 将完成块外的代码移动到成功块中,然后成功执行。

  2. 创建一个方法并将responseObject作为参数传递给它。 Onsuccess使用responseObject作为参数调用此方法。请勿在完成请求

  3. 之外做任何事情

    如果您需要任何帮助,请告诉我