1

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];
}
Regexident
  • 29,441
  • 10
  • 93
  • 100
Frames84
  • 177
  • 1
  • 3
  • 11
  • What is the error you experience? Also what do you mean by 'copy the access code into here', do you mean `[arr copy];`? – Alfonso Apr 08 '10 at 13:08
  • by access code i mean if I take the three lines from didSelectRowAtIndexPath and put them under the last line in viewDidLoad I am able to pull the data out. But in the didSelectRowAtIndexPath it errors. The error i get is EXC_BAD_ACCESS – Frames84 Apr 08 '10 at 13:24

4 Answers4

0

Maybe you should try the following (create a temporary array, pass this to the property method and release it afterwards):

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

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.

schaechtele
  • 1,130
  • 1
  • 9
  • 12
  • my property is set like this: @property (nonatomic, retain) NSArray *list; - which i think is correct. When I do your code in the viewDidLoad event I get an NSCFArray with 10 object of NSCFDictionary. I can then pull out the values for any of the 10 index in that event. But then when I try and access the self.list property in the didSelectRowAtIndexPath by doing NSArray val1 = [self.list objectAtIndex:6]; NSString *val11 = [val1 valueForKey@"title"]; I get an EXC_BAD_ACCESS error – Frames84 Apr 08 '10 at 13:02
0

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.

Alfonso
  • 8,386
  • 1
  • 43
  • 63
  • @Frames84 Maybe you should use a NSDictionary for your object vals. – schaechtele Apr 08 '10 at 11:44
  • i'm using code.google.com/p/json-framework to connect to my JSON Feeds. My feed comes back like this [ { "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" } ] my understanding is I can either return a NSArray or a NSDictornay. I'm loading the titles into my table view, then when I select a title I want to goto a detail page and display rest – Frames84 Apr 08 '10 at 12:54
0

I doubt vals is an array. Based on the example you've shown as JSON exmaple, it should be a dictionary.

NSDictionary *vals = [list objectAtIndex:row];

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:

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

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

petershine
  • 3,190
  • 1
  • 25
  • 49
0

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 ("").

Scott Corscadden
  • 2,831
  • 1
  • 25
  • 43