代码之家  ›  专栏  ›  技术社区  ›  bugfixr

为什么我的变量超出范围?

  •  1
  • bugfixr  · 技术社区  · 15 年前

    我在AppDelegate类中定义了NSMutableArray:

    NSMutableArray *devices;
    

    我从didFinishLaunchingWIthOptions方法中的类填充数组:

    Devices *devs = [[Devices alloc] init];
    self.devices = [devs getDevices];
    

    getDevices方法解析一个json字符串并创建一个设备对象,将其添加到数组中:

    NSMutableArray *retDevices = [[NSMutableArray alloc] initWithCapacity:[jsonDevices count]];
    
    for (NSDiectionary *s in jsonDevices) {
        Device *newDevice = [[Device alloc] init];
        device.deviceName = [s objectForKey:@"name"];
    
        [retDevices addObject: newDevice];
    }
    
    return retDevices;
    

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    Device *d = (Device *)[appDelegate.devices objectAtIndex:indexPath.row];
    
    cell.label.text = d.deviceName;
    

    最初填充tableview时,它会工作。但是,当我滚动列表时,cellForRowAtIndexPath再次执行时,d.deviceName会抛出一个错误,因为它超出了范围。

    提前谢谢。

    1 回复  |  直到 15 年前
        1
  •  4
  •   Jeremy W. Sherman    15 年前

    如果这确实是内存管理的问题,那么回答这些问题就可以得到答案:

    • deviceName 宣布为 retain copy 财产由 Device 接口声明?
    • -deviceName 合成的?如果没有,如何实施?
    • devices 保持 应用委托类的接口声明的属性?
    • 设备 合成的?如果没有,如何实施?