views:

597

answers:

4

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?

A: 

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
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
A: 

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.

frenetisch applaudierend
@Frames84 Maybe you should use a NSDictionary for your object vals.
schaechtele
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
A: 

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];


}
Frames84
When i inspect 'self.list' from ---- NSArray *stream = (NSArray *) [self.list objectAtIndex:row]; ---- it says 10 key/values pairs but then *stream object count is 0.. grrrrr
Frames84
A: 

Ended up going here http://stackoverflow.com/questions/1438860/nsarray-becomes-nscfarray-when-passed

and found out NSСFArray is subclass of NSMutable Array

so when I changed NSArray List to an NSMutableArray it appears to work.

Frames84