问题描述
我已将名为fid"的 NSString 值设置为 responseObject(见下文).responseObject 在 NSLog'd 时返回我想要的数据,但是由于某种原因,当我 NSLog 进一步向下查找时,它是空的.我错过了什么吗?见代码:
I've set the value of my NSString called "fid" to responseObject (see below). responseObject returns the data I want when NSLog'd, but for some reason when I NSLog fid further down, it's empty. Am I missing something? See code:
.h
@property (nonatomic, assign) 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!");
}];
NSDictionary *bodyValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:_itemDescrip.text, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *languageDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:bodyValues] forKey:@"und"];
[nodeData setObject:languageDict forKey:@"body"];
[nodeData setObject:@"storage_item" forKey:@"type"];
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"];
[DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Node saved!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Node did not save!");
}];
推荐答案
您正在调用异步方法.
在 fileSave 方法完成之前很久就执行了日志语句进一步向下".fileSave 的两个回调将在遥远的未来某个时候(可能是几秒钟后)被调用.
The log statement "further down" is executed long before the fileSave method is completed. The two callbacks of fileSave will be called sometime in the distant future (could be many seconds from now).
无论你想做什么需要 fid 都必须在 fileSave 的完成块中完成.
Whatever you want to do that needs fid must be done in the completion block of fileSave.
这篇关于NSString 的值返回 null,但不确定为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!