Variable Scope issue
It may be because this has been a long day... but I am having with some basic scope issues. I am creating an object, passing it in to a delegate method, and adding it to an array wihin the method.
When I check the value of device within the method, it contains the device information. Here is the code for the delegate function in the class that registered the delega开发者_运维知识库te:
- (void) newAmeriscanDevice:(AmeriscanDevice *)device {
if (!self.deviceArray)
self.deviceArray = [[NSMutableArray alloc] init];
// add the newly created device...
[self.deviceArray addObject:device];
}
This method is within the same class of the earlier function. The deviceArray shows that it contains one object (supposed to the the driver object from above). When I look at the value of the device object in here, it is always 0x0.
- (void) endDevices:(NSNumber *)numberOfDevices {
// get out of here is there is no device in the device array
if (!self.deviceArray)
return;
// lets sort the array by order of the devices sort order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptorArray = [NSArray arrayWithObject:sortDescriptor];
// the array should now be sorted correctly...
[self.deviceArray sortUsingDescriptors:sortDescriptorArray];
// we now have data -- so.... lets reload the table
[self.tableView reloadData];
}
So.... any idea on how to make sure the object in the array retains its values?
Thanks All
Mike
write code some thing like this
- (void) newAmeriscanDevice:(AmeriscanDevice *)device {
if (!self.deviceArray)
{
NSMutableArray *tempArray= [[NSMutableArray alloc] init];
self.deviceArray=tempArray;
[tempArray release];
}
[self.deviceArray addObject:device];
}
And check some where you are releasing the array or any reference which having the same location(any other array you have released in which you copy the array by using '=' operator).release it in dealloc.
I made a mistake with my assessment. I was looking at the debugger values when hovering over the array. It would show that there was one object and the object pointer was 0x0. I changed the code:
- (void) endDevices:(NSNumber *)numberOfDevices {
// get out of here is there is no device in the device array
if (!self.deviceArray)
return;
NSLog(@"Array Count: %i", [self.deviceArray count]);
for (id object in self.deviceArray) {
if (object == nil) {
NSLog(@"Nil Object");
} else {
AmeriscanDevice *dev = (AmeriscanDevice *)object;
NSLog(@"Device: %@", [dev description]);
}
}
// lets sort the array by order of the devices sort order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptorArray = [NSArray arrayWithObject:sortDescriptor];
// the array should now be sorted correctly...
[self.deviceArray sortUsingDescriptors:sortDescriptorArray];
// we now have data -- so.... lets reload the table
[self.tableView reloadData];
}
Once I changed this code, it showed that the object in the array was in face the proper type object.
My problem was in the display of a table. Apparently the cellForRowAtIndexPath method is not being called in the table when I called the reloadData or when the view is first shown. I created this table view using xcode 4, so I am heading into the xib file to see whats not linked :)
精彩评论