I have an POST request block in one of my functions and I want to change the value of an NSString object inside the block.
我的一个函数中有一个POST请求块,我想更改块内的NSString对象的值。
I realise that normally one could just prefix the variable __block
, but in my case, want to change the value of an NSString object by directly referencing its parameter.
我意识到通常可以只为变量__block添加前缀,但在我的情况下,想要通过直接引用其参数来更改NSString对象的值。
Here's a code skeleton, with relevant comments.
这是一个代码框架,带有相关注释。
- (void)getItemInformation:(NSString *)inputString{
//setup stuff
[manager POST:@"http://foo.com/iphone_item_string/"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//change inputString directly here
inputString = (NSString *)responseObject[0];
//this of course gives an error, but I'm
//unsure of how to use __block with a parameter
}
//foo
}
];
}
And a segment from the calling function
和来自调用函数的段
currentEntryData.foo = "lorem";
NSString *retrieveString;
[self getItemInformation:retrieveString];
currentEntryData.bar = retrieveString;
currentEntryData.oof = "ipsum";
1 个解决方案
#1
6
You can use blocks
你可以使用块
- (void)getItemInformationWithCallback:(void(^)(NSString *resultString))callback {
[manager POST:@"http://foo.com/iphone_item_string/"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Here you call the callback block
//
NSString *newString = (NSString *)responseObject[0];
if (callback)
callback(newString)
}
}];
}
And that's how you get the string back
这就是你如何获得字符串
[self getItemInformationWithCallback:^(NSString *resultString) {
NSLog(@"%@", resultString);
}];
#1
6
You can use blocks
你可以使用块
- (void)getItemInformationWithCallback:(void(^)(NSString *resultString))callback {
[manager POST:@"http://foo.com/iphone_item_string/"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Here you call the callback block
//
NSString *newString = (NSString *)responseObject[0];
if (callback)
callback(newString)
}
}];
}
And that's how you get the string back
这就是你如何获得字符串
[self getItemInformationWithCallback:^(NSString *resultString) {
NSLog(@"%@", resultString);
}];