加载后iPhone xcode阵列失去状态
是的,我到处找了找,什么也找不到加载后iPhone xcode阵列失去状态,iphone,ios,objective-c,cocoa-touch,nsarray,Iphone,Ios,Objective C,Cocoa Touch,Nsarray,是的,我到处找了找,什么也找不到 @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
@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
}
好的,我已经用了一些代码来尝试提供尽可能简单的代码,忽略输入错误和内存泄漏
基本上,当我选择一行时,我无法从我的“列表”数组对象中获取数据。
有人能帮我吗
这是我的全部代码 我使用的是我的
JSON
连接
my 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];
}
也许您应该尝试以下操作(创建一个临时数组,将其传递给属性方法,然后将其释放):
确保使用(非原子,保留)或(非原子,复制)设置NSArray列表的属性。此外,您还必须在此类的dealloc方法中释放NSArray列表。似乎您试图使用键值编码从数组中读取对象,但这不会起作用(至少不会达到预期效果)。如果在该位置获得的对象实际上是一个数组,则需要使用
objectAtIndex:
访问元素
尝试在行
NSString*id=[vals valueForKey:@“id]”处设置断点编码>并检查VAL,以查看此对象是否包含您期望的内容。我怀疑VAL
是否为数组。根据您显示为JSON exmaple的示例,它应该是一个字典
NSDictionary *vals = [list objectAtIndex:row];
此外,对于仅在范围内分配和使用的变量,不需要使用retain
你为什么不使用:
NSString *id = [vals objectForKey:@"id"];
要从dictionary对象中获取某些内容,请使用objectForKey
,而不是valueForKeyvalueForKey
用于与将对象转换为其他用途相关的其他目的此外,请注意,如果元素少于预期,JSON有时会以不同的结构返回。例如,如果有两个字典返回,您可能会得到一个数组,但如果只有一个,它就是直接的键/值结构(dictionary)。我见过在返回零结果的情况下,它会返回一个空字符串(“”)。@Frames84也许您应该为对象VAL使用NSDictionary。我正在使用code.google.com/p/json-framework连接到我的json提要。我的反馈是这样返回的[{“id”:“7812”,“title”:“title1”,“全文”:“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。我正在将标题加载到我的表视图中,然后当我选择一个标题时,我想转到详细信息页面并显示restmy属性,设置如下:@property(nonatomic,retain)NSArray*list;-我认为这是正确的。当我在viewDidLoad事件中执行您的代码时,我得到一个NSCFArray,其中包含10个NSCFDictionary对象。然后,我可以提取该事件中10个索引中任何一个的值。但是当我尝试通过执行NSArray val1=[self.list objectAtIndex:6]访问didSelectRowAtIndexPath中的self.list属性时;NSString*val11=[val1 valueForKey@“title”];我收到一个EXC\u BAD\u访问错误您遇到了什么错误?“将访问代码复制到此处”是什么意思,是指[arr copy]代码>?通过访问代码,我的意思是如果我从didSelectRowAtIndexPath中取出三行,并将它们放在viewDidLoad中的最后一行下,我就能够取出数据。但在DidSelectRowatindex路径中,它会出错。我得到的错误是EXC\u访问错误
NSString *id = [vals objectForKey:@"id"];