Reputation: 45
I'm developing a small iOs application which retrieves information from a server, and I found the pretty useful NSURLSessionDataTask.
First I used a @property (nonatomic, strong) NSMutableArray *objectArray;
which I called in my method:
- (void) createObjectsArrayFromUrl: (NSString *) url {
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if(error) {
//error handling
}
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableDictionary *jsonDataDictionary = [objectModel getJsonData];
self.objectArray = [objectModel arrayFromDictionary:jsonDataDictionary];
[[self collectionView] reloadData];
});
}];
[task resume];
}
And it all went smooth. Now I want to create a universal method, for more arrays, and I thought to do it by passing the array to the method, and updating it inside, like this:
- (void) createObjectsArrayFromUrl: (NSString *) url inArray: (NSMutableArray *) objectArray{
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if(error) {
//error handling
}
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableDictionary *jsonDataDictionary = [objectModel getJsonData];
objectArray = [objectModel arrayFromDictionary:jsonDataDictionary];
[[self collectionView] reloadData];
});
}];
[task resume];
}
but it gives me the error in the title in the following line: objectArray = [objectModel arrayFromDictionary:jsonDataDictionary];
and I don't know how to proceed.
So my idea is, instead of creating a method for every array I have, like before, I want to pass the array as a variable and update it in the method.
How to achieve this?
Thanks.
Upvotes: 3
Views: 8104
Reputation: 3141
Instead of
objectArray = [objectModel arrayFromDictionary:jsonDataDictionary];
try
[objecyArray removeAllObjects];
[objectArray addObjectsFromArray:[objectModel arrayFromDictionary:jsonDataDictionary]];
This is because the variable coming in does not have __block
in front of it thus allowing it to be used in a block, this is only available for local variables and not parameters. I believe __block
does so under the hood stuff to allowing to be referenced inside a block.
Upvotes: 1
Reputation: 27448
If you want to access variable in block then you should assign it as block first like __block NSArray *arr;
. then you can access that variable in block means in your completion handler.
In your code you can add one line at start of the method body like,
__block NSArray *arr = objectArray;
And then use this arr
in block like,
arr = [objectModel arrayFromDictionary:jsonDataDictionary];
Hope this will help :)
Upvotes: 17