Terrel Gibson
Terrel Gibson

Reputation: 481

Declaring Variables in Blocks, Objective C

thread my project but using queue's and blocks but I get an error when I try to queue the code. I know that you cannot queue UI elements in blocks so I am avoiding that but the error I get is when I do call a UI element outside of the block it says that the variable is not declared although it is declared inside the block. Here is the code. The code is a UITableView method and it just takes an array sorts it and displays it.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create an instance of the cell
UITableViewCell *cell;
cell = [self.tableView dequeueReusableCellWithIdentifier:@"Photo Description"];

if(!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Photo Description"];

// set properties on the cell to prepare if for displaying
//top places returns an array of NSDictionairy objects, must get object at index and then object for key

//Lets queue this
dispatch_queue_t downloadQueue = dispatch_queue_create("FlickerPhotoQueue", NULL);
dispatch_async(downloadQueue, ^{

//lets sort the array

NSArray* unsortedArray = [[NSArray alloc] initWithArray:[[self.brain class] topPlaces]] ;
//This will sort the array
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"_content" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:descriptor];                                   
NSArray *sortedArray = [[NSArray alloc] init];
sortedArray = [unsortedArray sortedArrayUsingDescriptors:sortDescriptors];

NSString * cellTitle = [[sortedArray objectAtIndex:self.location] objectForKey:@"_content"]; 

NSRange cellRange = [cellTitle rangeOfString:@","];

NSString * cellMainTitle = [cellTitle substringToIndex:cellRange.location];

});
dispatch_release(downloadQueue);  

//Claims that this are not declared since they are declared in the block
cell.textLabel.text = cellMainTitle;
//This isnt declared either
NSString* cellSubtitle = [cellTitle substringFromIndex:cellRange.location +2];

cell.detailTextLabel.text =  cellSubtitle;
self.location++;
return cell;
}

I managed to get the program to work by moving the dispatch release to the very end of the block of code and then having the UI interface be declared in the main thread by calling dispatch_get_main_queue. Thanks for all the help

Upvotes: 0

Views: 1749

Answers (2)

Andy Ibanez
Andy Ibanez

Reputation: 12254

If I am understanding your problem correctly, you are declaring a variable inside a block and trying to use it outside.

That won't work. Variables declared inside blocks are limited to the block's scope. If you create them inside the block, you are limited to using them in the block and you can't use them anywhere else.

A better idea will be to create the variable you want to use outside the block. If you want to modify the variable in the block, use the __block keyword.

__block UITableViewCell *someCell;
//__block tells the variable that it can be modified inside blocks.
//Some generic block
^{
//initialize the cell here.
}

Upvotes: 6

dans3itz
dans3itz

Reputation: 1615

The variable being defined inside the block is local to that block and thus does not exist outside of it. You can pre-define these variables before entering the block, however this is entering a specialized territory and requires use of an extension when defining these variables. Look up "__block" storage type in Organizer. I did not spend too much time reviewing your code to know if this is sane advice given the details.

Upvotes: 1

Related Questions