Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSMutableArray对象所有权_Ios_Objective C_Nsmutablearray_Uitableview - Fatal编程技术网

Ios NSMutableArray对象所有权

Ios NSMutableArray对象所有权,ios,objective-c,nsmutablearray,uitableview,Ios,Objective C,Nsmutablearray,Uitableview,我在TableView控制器中使用了NSMutableArray来保存TableView中显示的项目。正在viewDidLoad方法中填充NSMutableArray - (void)viewDidLoad { [super viewDidLoad]; [self populateItems]; } - (void)populateItems { for (int i = 0; i < numberOfItems; i++) { Item *item

我在TableView控制器中使用了NSMutableArray来保存TableView中显示的项目。正在viewDidLoad方法中填充NSMutableArray

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self populateItems];
}
- (void)populateItems {
    for (int i = 0; i < numberOfItems; i++) {
        Item *item = [Item createItem];
        [self.items addObject:item];
        item = nil;   // to ensure that newly created object is not pointed by item and only owned by NSMutableArray
    }
}
在这个方法中,我从NSMutableArray获取对象,并在再次将其插入数组之前从NSMutableArray中删除该对象

我的问题是- 如果NSMutable是其中对象的唯一所有者 然后在从数组中删除它之后,不应该有人指向该项对象。 因此,它本应被释放,但事实并非如此。它正在成功地再次插入到tableview中。为什么它没有发生

我相信行Item*Item=[self.items objectAtIndex:fromIndexPath.row];
项指针不是fromIndexPath中存在的对象的所有者,下面一行声明了对该对象的强引用

Item *item = [self.items objectAtIndex:fromIndexPath.row];
这允许您将其从阵列中删除并再次插入

如果出于某种原因,您希望项目不是一个强有力的参考,您可以这样做:

_weak Item *item = [self.items objectAtIndex:fromIndexPath.row];

假设您启用了ARC,并且应该启用ARC,则这一行代码:

Item *item = [self.items objectAtIndex:fromIndexPath.row];
将默默地为您保留该对象,直到其不再使用

如果禁用了ARC,则有三种可能性:

1) 数组以外的其他内容保留了该对象,这可能是内存泄漏,您应该进一步调查

2) 对象已被释放,但在该内存位置没有任何内容被覆盖,因此它仍然可以完美地工作。讨厌!这是ARC要解决的问题类型

3) 苹果可能不会在将对象从阵列中移除后立即释放该对象,这可能是出于性能原因。苹果公司为提高性能和电池寿命而采取的大量未经记录且通常怪异的内存管理行为,尤其是在iOS上,在iOS上,他们控制CPU硬件,不必太担心向后兼容性


总之,如果尚未启用ARC,则应启用ARC。你不能依赖被“摧毁”的物体。苹果实际上并没有承诺当你从数组中移除一个对象时它会被销毁,他们只是告诉开发者他们的代码必须假设它可能已经被销毁。

你有一个局部变量
。默认情况下,变量是强的。因此,
item
是一个很强的变量。只要该变量存在,它将获得保存到它的对象的所有权

局部变量只在其作用域存在时才存在。在此代码中:

-(void) someMethod
{
  NSMutableArray *array1 = [NSMutableArray new];
  {
    NSMutableArray *array2 = [NSMutableArray new];
    [array2 addObject: @"Foo"];
    [array1 addObject: @"Bar"];
  }
  //At this point, array2 no longer exists, so the string "Foo" is deallocated"

  //array1 is still in scope, so the string "bar" still exists
  __weak NSMutableArray *array3;

  //The line below is useless since the array is released as soon as it is created
  array3 = [NSMutableArray new];  
}
NSMutableArray1在方法的生命周期内存在,因为它的局部变量是强的(默认值)

变量array2也很强,但它的作用域是包围它的大括号集。一旦到达右大括号,代码就会退出该范围,因此该变量不再为任何人所有,并被释放


这有用吗

成功了。通过将_弱添加到Item对象进行检查,应用程序崩溃,表示无法添加nil对象,这是由于Item已设置为nil,因为我们从数组中删除了该项
-(void) someMethod
{
  NSMutableArray *array1 = [NSMutableArray new];
  {
    NSMutableArray *array2 = [NSMutableArray new];
    [array2 addObject: @"Foo"];
    [array1 addObject: @"Bar"];
  }
  //At this point, array2 no longer exists, so the string "Foo" is deallocated"

  //array1 is still in scope, so the string "bar" still exists
  __weak NSMutableArray *array3;

  //The line below is useless since the array is released as soon as it is created
  array3 = [NSMutableArray new];  
}