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
Iphone 数组nil(即使addObject:)_Iphone_Objective C_Cocoa Touch_Nsmutablearray - Fatal编程技术网

Iphone 数组nil(即使addObject:)

Iphone 数组nil(即使addObject:),iphone,objective-c,cocoa-touch,nsmutablearray,Iphone,Objective C,Cocoa Touch,Nsmutablearray,可能重复: 一切都更新了 此应用程序是一个带有选项卡栏控制器的表视图。我正在记录数组的计数:arrayOfFavourites,即使我添加了一个对象,但它仍然有一个nil值,我的相关代码,显示的所有对象都在代码中分配和初始化,在前面或现在,有些是实例,有些是属性: ListViewController.m: WebViewController.m: 你摇到最喜欢的手机 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)

可能重复:

一切都更新了

此应用程序是一个带有选项卡栏控制器的表视图。我正在记录数组的计数:arrayOfFavourites,即使我添加了一个对象,但它仍然有一个nil值,我的相关代码,显示的所有对象都在代码中分配和初始化,在前面或现在,有些是实例,有些是属性:

ListViewController.m: WebViewController.m: 你摇到最喜欢的手机

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    cellToPassOn = nil;

    NSLog(@"Favouriting"); // YES I KNOW SPELLING

    // This is pretty simple, what we do is we take the cell we touched and take its title  and link 
    // then put it inside an array in the Favourites class

    Favourites *fav = [[Favourites alloc] init];
    ListViewController *list = [[ListViewController alloc] init];
    [self setCellToPassOn: [list CELL]];

    if (!item) {
        NSLog(@"NILLED ITEM");
    }

    [[fav arrayOfFavourites] addObject:[item autorelease]];
    [fav setCell: cellToPassOn];
}
最喜欢的。m: 最爱: 类,位于选项卡栏控制器上

WEBVIEWCONROLLER:

不同WEB视图的控制器

LISTVIEWCONTROLLER:

数据提供者

实际上,当我摇动时,我重新加载表视图数据,并将一个对象添加到收藏夹的数组中,计数为1。。。。好的但当我在另一篇文章中再次摇晃时,当我按下不同的单元格时,我的应用程序具有不同的网络视图。它仍然是1。。。奇怪的如果我转到favorites类,则该数组仍为1。。好啊正如你所看到的,我正在将ArrayOfVourites计数返回到numberOfRowsInSection,它是1,但没有出现单元格,并且从未使用NSLOG调用cellForRowAtIndexPath。为什么会发生这种情况?我非常恼火

在您的收藏夹.m numberOfRowsInSection函数中,看起来您应该执行以下操作:

if(arrayOfFavourites == NULL)
{
    arrayOfFavourites = [[NSMutableArray alloc] init];
}

因为您正在重新初始化,并且每次调用numberOfRowsInSection时都可能会泄漏,每次调用该部分时,表都需要知道它必须显示多少行—即非常频繁。

您每次通过时都会创建并销毁一个新的Favorites对象—MotionStart:withEvent:,每次通过-tableView:numberOfRowsInSection:,都会创建一个新数组。如果希望数据在这些事件之后保持不变,则需要保留对象。

让我们回顾一下代码的这一部分:

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

cellToPassOn = nil;
NSLog(@"Favouriting"); // YES I KNOW SPELLING

// This is pretty simple, what we do is we take the cell we touched and take its title  and link 
// then put it inside an array in the Favourites class

// HERE you are creating a NEW fav
Favourites *fav = [[Favourites alloc] init];
// HERE you are creating a NEW list
ListViewController *list = [[ListViewController alloc] init];
//  SO HERE what is "CELL" doing, returning some constant or static object?
[self setCellToPassOn: [list CELL]];

// HERE what is item and where does it come from
if (!item) {
    NSLog(@"NILLED ITEM");
}

// Here you take an array of an object you just created and autoreleasing the item
// this is not the regular way to handle memory management in Cocoa, 
// depending on what you are doing to item else where you could get item == deallocated pretty soon
[[fav arrayOfFavourites] addObject:[item autorelease]];
[fav setCell: cellToPassOn];

HERE you are releasing fav
[fav release];
HERE fav don't exist anymore as well as the array to which you've added something to it.
[list release];
item = nil;
}
除非我没有正确地阅读您的代码,否则我会觉得您试图对volatile对象进行持久化

您需要为这些对象创建一个@property,以便在一次方法调用中存活更长的时间。 每次创建一个新对象时,它都是新的,并且不知道以前的对象

如果我们看一下这个代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// HERE everytime the tableview is asking for the numberOfRowsInSection, you create a new array 
// And that new empty array is replacing your old one.  
// That is your "weird" thing, it's doing what you are asking it to do, set it back to a new empty array.
arrayOfFavourites = [[NSMutableArray alloc] init];

NSLog(@"ROWS NO.");
NSLog(@"%i", [arrayOfFavourites count]);

return [arrayOfFavourites count];
}

是的,对你来说这也是+1。好的:这是在我摆脱了这个版本之后发生的事情:好的,我摆脱了这个版本,现在是零。发生了什么:如果我在一个网络视图中抖动,计数变成一。。。。很好,如果我在不同的网络视图中摇晃,仍然是一个而不是两个。。。奇怪的是,当我进入收藏夹查看时,计数仍然是一而不是零。。。好的在numberOfRowsInSection中记录的计数,我返回数组计数,它是1,但没有单元格出现,因为从未调用cellForRowAtIndexPath我在其中放置了一个NSLOG。顺便说一句,numberOfRowsInSection中arrayOfFavourites的分配和初始化是在堆栈溢出中粘贴代码时发生的意外。。。我去掉了它,我不需要它,因为我正在将web view controller中的数组arr分配给Favorites中的arrayOfFavourites,所以它不应该是nilok我编辑了这个问题,以显示我的新代码仍然不起作用。。。。。item是RSSItem类的一个对象,它接受XML并解析它并找到它的项。该对象在ListViewController中生成,并传输到添加到阵列的WebViewController。listviewcontroller中的单元格是一个UITableViewCell,我需要它来在收藏夹中查看内容。无论如何,这里发生的事情是,我在ListViewController中有一个tableView,当我从中按下一个单元格时,它会转到我从apple rss提要的XML解析的一篇文章……我想收藏这篇文章,所以当你在文章中时,你会摇晃它,并将其作为一个单元格添加到收藏夹中……我如何做到这一点。。。。。无论如何,检查我的编辑question@user973985listviewcontroller中的单元格是一个UITableViewCell,我需要它来在收藏夹中查看内容。单元格是一个常量,或者是您在初始化状态下创建的东西,因为您正在对新分配的对象调用它。从你过去的代码来看,你在这个范例上有问题。您无法返回以下内容:CELL=[tableView cellforrowatinexpath:indexath];因为它是在另一个对象上设置的。你应该缓存重建一个单元格所需的内容,而不是单元格本身。。。。。关于阵列的任何想法在收藏夹中到达时为零,并且不会超过一个??webViewController=零的计数;和webViewController=[[webViewController alloc]init];
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

cellToPassOn = nil;
NSLog(@"Favouriting"); // YES I KNOW SPELLING

// This is pretty simple, what we do is we take the cell we touched and take its title  and link 
// then put it inside an array in the Favourites class

// HERE you are creating a NEW fav
Favourites *fav = [[Favourites alloc] init];
// HERE you are creating a NEW list
ListViewController *list = [[ListViewController alloc] init];
//  SO HERE what is "CELL" doing, returning some constant or static object?
[self setCellToPassOn: [list CELL]];

// HERE what is item and where does it come from
if (!item) {
    NSLog(@"NILLED ITEM");
}

// Here you take an array of an object you just created and autoreleasing the item
// this is not the regular way to handle memory management in Cocoa, 
// depending on what you are doing to item else where you could get item == deallocated pretty soon
[[fav arrayOfFavourites] addObject:[item autorelease]];
[fav setCell: cellToPassOn];

HERE you are releasing fav
[fav release];
HERE fav don't exist anymore as well as the array to which you've added something to it.
[list release];
item = nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// HERE everytime the tableview is asking for the numberOfRowsInSection, you create a new array 
// And that new empty array is replacing your old one.  
// That is your "weird" thing, it's doing what you are asking it to do, set it back to a new empty array.
arrayOfFavourites = [[NSMutableArray alloc] init];

NSLog(@"ROWS NO.");
NSLog(@"%i", [arrayOfFavourites count]);

return [arrayOfFavourites count];
}