iPhone xcode 数组加载后丢失状态
是的,我已经搜索了一圈,但找不到任何东西。
@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];
}
Right i've had a search around and can't find anything.
@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
}
Right i've taken some of the code to try and provide it as simple as possible, ignore typo's and memory leaks this is sorted.
Basically when I select a row I can't get data out of my 'list' array object.
Please can anyone help me out?
This is my full code
I'm using http://code.google.com/p/json-framework/ for my JSON
connection
my Json is returned in this format:
[
{
"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
in here declare:
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许您应该尝试以下操作(创建一个临时数组,将其传递给属性方法,然后释放它):
确保使用(nonatomic,retain)或(nonatomic,copy)设置 NSArray 列表的属性。此外,您还必须在此类的 dealloc 方法中释放 NSArray 列表。
Maybe you should try the following (create a temporary array, pass this to the property method and release it afterwards):
Make sure you set the property for your NSArray list with (nonatomic, retain) or (nonatomic, copy). Also you have to release the NSArray list in the dealloc method of this class.
似乎您尝试使用键值编码从数组中读取对象,但这不起作用(至少它不会执行您期望的操作)。如果您在该位置获取的对象确实是一个数组,则需要使用
objectAtIndex:
访问元素。尝试在
NSString *id = [vals valueForKey:@"id"];
行设置断点并检查 vals 以查看该对象是否符合您的预期并包含您所期望的内容。Seems like you try to read objects from an array using key-value coding, which will not work (at least it won't do what you expect). If the object you get at that position is really an array, you need to access the elements with
objectAtIndex:
.Try setting a breakpoint at the line
NSString *id = [vals valueForKey:@"id"];
and inspect vals to see if this object is and contains what you expect.我怀疑 vals 是一个数组。根据您显示为 JSON exmaple 的示例,它应该是一本字典。
另外,对于仅在作用域内分配和使用的变量,您不需要使用保留;
为什么不使用:
要从字典对象中获取某些内容,请使用
objectForKey
,而不是valueForKey。valueForKey
用于与将对象转换为其他用途相关的其他目的I doubt
vals
is an array. Based on the example you've shown as JSON exmaple, it should be a dictionary.Also, you don't need to use retain, for a variable that is allocated and used only inside the scope;
And Why don't you use:
To get something from a dictionary object, you use
objectForKey
, not valueForKey.valueForKey
is used for other purposes related to transforming the object for other use另请注意,如果元素少于您的预期,有时 JSON 会以不同的结构返回。例如,如果有两个字典返回,您可能会得到一个数组,但如果只有一个,它只是直接的键/值结构(字典)。我见过在返回零结果的情况下它可以返回空字符串(“”)。
Also, note that JSON will come back as different structures sometimes if there are less elements than you expect. For example, if there were two dictionaries that come back, you might get an array, but if only one, it is just the straight key/value structure (dictionary). I've seen in cases where zero results are returned it can return an empty string ("").