gpt4 book ai didi

iOS NSDictionary 传递函数

转载 作者:行者123 更新时间:2023-11-28 22:34:31 24 4
gpt4 key购买 nike

我有一个名为 SecondViewController 的 View Controller ,当用户选择要阅读的文章时,我想导航到 ThirdViewController 并传递带有文章数据的 NSDictionary。

- (void) touchButtonBlock:(NSInteger) select
{

NSLog(@"touchButtonBlock");
NSDictionary *dict;
switch (select) {
case 0:
{
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *val = [[NSMutableDictionary alloc] init];
[val setObject:@"article360.html" forKey:@"page"];
[val setObject:@"img.png" forKey:@"img"];
[val setObject:@"art1" forKey:@"name"];

[tmpDict setObject:val forKey:[NSNumber numberWithInt:0]];

val = [[NSMutableDictionary alloc] init];
[val setObject:@"article360.html" forKey:@"page"];
[val setObject:@"img.png" forKey:@"img"];
[val setObject:@"art2" forKey:@"name"];

[tmpDict setObject:val forKey:[NSNumber numberWithInt:1]];

dict = [NSDictionary dictionaryWithDictionary:tmpDict];

//[tmpDict release];
//[val release];
break;
}
NSDictionary *immDict = [NSDictionary dictionaryWithDictionary:dict];

ArticleSelectListViewController *selectorView = [[ArticleSelectListViewController alloc] initWithArticlesData:immDict];
[self.navigationController pushViewController:selectorView animated:YES];
[selectorView release];
}

这里我给 NSDicitionary 赋值:

- (id)initWithArticlesData:(NSDictionary *)data
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
articles = data;

...
}

但在用户交互文章变量之后调用的其他函数中,变量似乎不正确 - 它不包含我的数据。

我哪里出错了?

最佳答案

复制字典然后赋值。

 - (id)initWithArticlesData:(NSDictionary *)data
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
articles = [data copy];

...
}

关于iOS NSDictionary 传递函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16461189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com