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 是否保留NSMutableArray的值?_Iphone_Objective C_Uitableview_Nsmutablearray - Fatal编程技术网

Iphone 是否保留NSMutableArray的值?

Iphone 是否保留NSMutableArray的值?,iphone,objective-c,uitableview,nsmutablearray,Iphone,Objective C,Uitableview,Nsmutablearray,我只是被卡住了。我有两个视图控制器,一个是“WatchListViewController”,第二个是“QuotesViewController”,首先我从WatchListViewController推到QuotesViewController,我的动作写在这里 -(IBAction)qoutes { QuotesViewController *nextController = [[QuotesViewController alloc] initWithQoutes:symbolFor

我只是被卡住了。我有两个视图控制器,一个是“
WatchListViewController
”,第二个是“
QuotesViewController
”,首先我从
WatchListViewController
推到
QuotesViewController
,我的动作写在这里

-(IBAction)qoutes
{
    QuotesViewController *nextController = [[QuotesViewController alloc] initWithQoutes:symbolForQoutes :exchangeForQoutes :appDelegate.s :code :exchange:exchangeCodeForQoutes];
    [self.navigationController pushViewController:nextController animated:YES]; 
}
我的
initWithQoutes
方法也很好地工作,然后在
ViewDidLoad
方法中,我通过套接字向服务器发送一些请求,它给出响应,并在我的函数中收到响应,该函数位于
WatchListViewController

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info)
{
    char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data); //getting data from server side in a char type veriable "buffer"
    NSString *result = [NSString stringWithUTF8String:buffer]; // getting data in a string from  my buffer
    if (strstr(buffer, "QRES") != NULL) { //if this is a qoutes response
        splits = [result componentsSeparatedByString:@"|"]; //separating string by "|"
        body = [splits objectAtIndex:1]; // choosing index "1"
        splits = [body componentsSeparatedByString:@","]; // separating by ","
        QuotesViewController *quotesViewController = [[QuotesViewController alloc] init];
        [quotesViewController initWithData:splits];
        return;
    }
}
收到响应后,它将转到带有参数数组“splits”的方法
initWithData
。我在initWithData中接收此数组,并将其分配给另一个数组(
generalArray
)现在,常规数组被分配给我的
tableView
,但在它重新加载tableView的
reloadData
之前,它的控制返回到
receiveData
,然后返回并再次返回到我的
QuotesViewController
并重新加载表视图,但此时我的数组(
generalArray
)丢失其值&未在表视图中加载值。 请帮帮我,我被困在这里了

initWithData方法的代码:

-(void)initWithData:(NSArray *)_general
{ 
general = [[NSMutableArray alloc] init]; //initializing array that can be accessed out of the class 
for (int i=0; i< [_general count]; i++) { 
[general addObject:[_general objectAtIndex:i]]; } 
NSLog(@"%@", general); [self generalValues]; 
} 
-(void)generalValues{ 

[generalTableView reloadData]; 
}
-(void)initWithData:(NSArray*)\u概述
{ 
general=[[NSMutableArray alloc]init];//初始化可在类外访问的数组
对于(int i=0;i<[[u一般计数];i++){
[general addObject:[[通用对象索引:i]];]
NSLog(@“%@”,一般);[自生成值];
} 
-(void)一般值{
[generalTableView重新加载数据];
}

您应该学习Objective-C中的内存管理。您使用
组件创建的数组通过该方法自动释放。因此,当您将它传递到QuotesViewController的
initWithData
方法中时,您需要使用类似

generalArray = [splits retain];

如果您不确定要做什么,请发布您的
initWithData
代码。

您正在C函数中创建一个
QuotesViewController
实例-

[quotesViewController initWithData:splits];
您没有存储对此控制器的引用。
QuotesViewController
的此实例很可能在
generalArray
中具有正确的值。但由于您没有将其存储以供以后使用,因此会丢失一个引用并导致内存泄漏

现在,如果继续创建
QuotesViewController
的另一个实例,它将不会有早期的
generalArray
数据,因为它是一个新实例。由于我不了解您的程序流程,我发现很难提出正确的前进方式,但将引用保存在函数中或数组本身是正确的。如果保存引用,只需将其推入的导航控制器中。如果保存数组,则可以在init方法中传递它

编辑

由于您谈论的是影响同一控制器,因此需要将引用传递给函数

void receiveData(CFSocketRef s, CFSocketCallBackType type,  CFDataRef address, const void *data, void *info, QuotesViewController *controller)
{
    char *buffer = (char *)CFDataGetBytePtr((CFDataRef)data); //getting data from server side in a char type veriable "buffer"
    NSString *result = [NSString stringWithUTF8String:buffer]; // getting data in a string from  my buffer
    if (strstr(buffer, "QRES") != NULL) { //if this is a qoutes response
        splits = [result componentsSeparatedByString:@"|"]; //separating string by "|"
        body = [splits objectAtIndex:1]; // choosing index "1"
        splits = [body componentsSeparatedByString:@","]; // separating by ","
        [controller initWithData:splits];
        return;
    }
} 
希望这可以做到。

在这段代码中:

QuotesViewController *quotesViewController = [[QuotesViewController alloc] init];
[quotesViewController initWithData:splits];
您正在初始化
quotesViewController
两次。第二次初始化时,没有存储返回值,因此引用丢失。相反,你应该写作

QuotesViewController *quotesViewController = [[QuotesViewController alloc] initWithData:splits];

尝试一下,看看是否有帮助。

你能清楚我在阅读时丢失了链接的最后一条语句吗。这里要回答的是代码-(void)initWithData:(NSArray*)_general{general=[[NSMutableArray alloc]init];//初始化可以从类外访问的数组(int i=0;i<[[u general count];i++){[general addObject:[[u general objectAtIndex:i]];}NSLog(@“%@”,general);[self-generalValues];}-(void)generalValues{[generalTableView reloadData];}你能把你的代码作为你问题的编辑吗?很难把所有的代码都放在这样的评论中阅读。@Mashhadi:你可以避免这样一个循环:用以下行将
\u general
复制到
general
你说得太对了,但是这个响应是在推后出现的。infect这个响应是由“ViewDidLoad”生成的请求。所以你把一个
QuotesViewController
实例推到前面,然后触发一个请求,该请求应该更新同一实例中的
general
数组,对吗?