在 Block 具有 运行 之前返回的 NSString 值

NSString value returning before Block has run

我正在尝试在块外使用块内生成的值 ("fid")。问题是值在块具有 运行 之前被拉取,因此值 returns 为(空),即使数据存在。有谁知道我怎样才能使这项工作?见代码:

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

您可以通过两种方式执行此操作:

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

  2. 创建一个方法并将 responseObject 作为参数传递给它。 Onsuccess 以 responseObject 作为参数调用此方法。不要在 request

  3. 的完成块下面做任何事情

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