【问题标题】:iPhone xcode array losing state after load [duplicate]加载后iPhone xcode数组丢失状态[重复]
【发布时间】:2010-04-08 10:47:58
【问题描述】:

是的,我已经四处搜索,找不到任何东西。

@synthesize list; // this is an NSArry

-(void) viewDidLoad {
   NSArray *arr = [self getJSONFeed];
   self.List = [arr retain];     // if i copy the access code into here it works fine.
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSArray *vals = [list objectAtIndex:row] retain];
    NSString *id = [vals valueForKey:@"id"]; // ERROR
}

是的,我已经采取了一些代码来尝试并尽可能简单地提供它,忽略拼写错误和内存泄漏,这是排序的。

基本上,当我选择一行时,我无法从“列表”数组对象中获取数据。 请问谁能帮帮我?


这是我的完整代码

我使用http://code.google.com/p/json-framework/ 连接我的JSON

我的 Json 以这种格式返回:

[
    {
        "id": "7812",
        "title": "title1",
        "fulltext": "main body text",
        "created": "2010-04-06 11:30:03"
    },
    {
        "id": "7813",
        "title": "title2",
        "fulltext": "main body text2",
        "created": "2010-04-06 11:30:03"
    }
]

MainNewsController.h

在这里声明:

NSArray *list:   
@property (nonatomic, retain) NSArray *list;

MainNewsController.m

@synthesize list;

-(void) viewDidLoad {
    NSArray *array =  [[NSArray alloc] initWithArray: [self downloadPublicJaikuFeed]];
    self.list = array;    
    NSArray *stream = [list objectAtIndex:6]; // can change index and it's fine
    NSString *vall = [stream valueForKey:@"title"]; // works fine    
    NSString *val1l = [stream valueForKey:@"id"]; // works fine
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MainNewsCellIdentifier = @"MainNewsCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier];    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: MainNewsCellIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row];
    NSDictionary *stream = (NSDictionary *) [list objectAtIndex:row];
    cell.textLabel.text = [stream valueForKey:@"id"];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    [stream release];
    return cell;
}

#pragma mark -    
#pragma mark Table Delegate Methods

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (childController == nil)    
        childController = [[MainNewsDetailController alloc] initWithNibName:@"DisclosureDetail" bundle:nil];
    childController.title = @"News Detail Button Pressed";    
    NSUInteger row = [indexPath row];
    NSArray *stream = (NSArray *) [self.list objectAtIndex:row];
    NSString *vall = [stream valueForKey:@"title"]; // error
    NSString *val1l = [stream valueForKey:@"id"]; // error
    childController.message = @"111";
    childController.title = @"222";
    [self.navigationController pushViewController:childController animated:YES];
}

【问题讨论】:

  • 您遇到的错误是什么?另外,“将访问代码复制到此处”是什么意思,您的意思是[arr copy];
  • 通过访问代码,我的意思是如果我从 didSelectRowAtIndexPath 中取出三行并将它们放在 viewDidLoad 的最后一行下面,我就能够提取数据。但是在 didSelectRowAtIndexPath 中它会出错。我得到的错误是 EXC_BAD_ACCESS

标签: iphone ios objective-c cocoa-touch nsarray


【解决方案1】:

也许你应该尝试以下方法(创建一个临时数组,将其传递给属性方法并在之后释放它):

 NSArray *arr = [[NSArray alloc] initWithArray:[self getJSONFeed]];
 self.list = arr;
 [arr release];

确保使用 (nonatomic, retain) 或 (nonatomic, copy) 设置 NSArray 列表的属性。另外你必须在这个类的 dealloc 方法中释放 NSArray 列表。

【讨论】:

  • 我的属性设置如下:@property (nonatomic, retain) NSArray *list; - 我认为这是正确的。当我在 viewDidLoad 事件中执行您的代码时,我得到一个带有 10 个 NSCFDictionary 对象的 NSCFArray。然后,我可以在该事件中提取 10 个索引中的任何一个的值。但是当我尝试通过执行 NSArray val1 = [self.list objectAtIndex:6] 访问 didSelectRowAtIndexPath 中的 self.list 属性时NSString *val11 = [val1 valueForKey@"title"];我收到 EXC_BAD_ACCESS 错误
【解决方案2】:

似乎您尝试使用键值编码从数组中读取对象,但这是行不通的(至少它不会达到您的预期)。如果你在那个位置得到的对象真的是一个数组,你需要用objectAtIndex:访问元素。

尝试在NSString *id = [vals valueForKey:@"id"]; 行设置断点并检查 vals 以查看此对象是否包含您所期望的内容。

【讨论】:

  • @Frames84 也许你应该为你的对象 vals 使用 NSDictionary。
  • 我正在使用 code.google.com/p/json-framework 连接到我的 JSON 提要。我的提要像这样返回 [ { "id": "7812", "title": "title1", "fulltext": "main body text", "created": "2010-04-06 11:30:03" }, { "id": "7813", "title": "title2", "fulltext": "main body text2", "created": "2010-04-06 11:30:03" } ] 我的理解是我可以返回一个 NSArray 或一个 NSDictornay。我正在将标题加载到我的表格视图中,然后当我选择标题时,我想转到详细信息页面并显示其余部分
【解决方案3】:

我怀疑vals 是一个数组。根据您显示为 JSON 示例的示例,它应该是一个字典。

NSDictionary *vals = [list objectAtIndex:row];

另外,对于仅在作用域内分配和使用的变量,您不需要使用保留;

你为什么不使用:

NSString *id = [vals objectForKey:@"id"];

要从字典对象中获取某些内容,请使用objectForKey,而不是 valueForKey。 valueForKey 用于与将对象转换为其他用途相关的其他目的

【讨论】:

    【解决方案4】:

    另外,请注意,如果元素少于您的预期,JSON 有时会以不同的结构返回。例如,如果有两个字典返回,你可能会得到一个数组,但如果只有一个,它只是直接的键/值结构(字典)。我见过在返回零结果的情况下,它可以返回一个空字符串(“”)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      相关资源
      最近更新 更多