可变范围问题

时间:2011-06-17 03:30:33

标签: iphone ipad

可能是因为这是漫长的一天......但我遇到了一些基本的范围问题。我正在创建一个对象,将其传递给委托方法,并将其添加到该方法的数组中。

当我检查方法中的设备值时,它包含设备信息。 以下是注册委托的类中委托函数的代码:

- (void) newAmeriscanDevice:(AmeriscanDevice *)device {
    if (!self.deviceArray)
        self.deviceArray = [[NSMutableArray alloc] init];

    // add the newly created device...
    [self.deviceArray addObject:device];
}

此方法与早期函数属于同一类。 deviceArray显示它包含一个对象(应该是上面的驱动程序对象)。当我在这里查看设备对象的值时,它总是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];

}

那么....关于如何确保数组中的对象保留其值的任何想法?

全部谢谢

麦克

2 个答案:

答案 0 :(得分:0)

写代码就像这样

 - (void) newAmeriscanDevice:(AmeriscanDevice *)device {
        if (!self.deviceArray)
        {
            NSMutableArray *tempArray= [[NSMutableArray alloc] init];
            self.deviceArray=tempArray;
   [tempArray release];
     }


        [self.deviceArray addObject:device];





    }

并检查一些释放数组的位置或任何具有相同位置的引用(您发布的任何其他使用'='运算符复制数组的数组)。在dealloc中释放它。

答案 1 :(得分:0)

我的评估错了。当我将鼠标悬停在数组上时,我正在查看调试器值。它会显示有一个对象,对象指针是0x0。我改变了代码:

- (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];

}

一旦我更改了这段代码,它就会显示数组中的对象面对正确的类型对象。

我的问题出在一张桌子上。显然,当我调用reloadData或首次显示视图时,表中没有调用cellForRowAtIndexPath方法。我使用xcode 4创建了这个表视图,所以我要进入xib文件以查看没有链接的内容:)