icekomo
icekomo

Reputation: 9466

Access a property from a custom object inside a NSArray

I have a custom object (historyObject) I created with a few properties. One of those being a NSString with the name of savedDate. I create the custom object set all the values that I need save it and place it inside an array (historyArray). From another VC I am using a tableview and want to populate the table views cell textLable with the custom object property savedDate.

I can NSLog out the historyArray, so I know the objects are there, I can see the names of them, but I'm finding it difficult to access a property of that object.

This is what I'm trying to use just to set an NSString to use later for the cell lableText:

NSString *cellLableText = [billDetails.historyArray objectAtIndex:indexPath.row] saveDate;

I feel like I am over looking some very simple, but can't see to figure it out.

********************************************** UPDATE *********

After some more trail and error I'm starting to wonder if I am encoding the decoding the object properly. When the History object is created I call this method on the history object

NSData *hisotryEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:historyObject];

And the history object conforms to NSCoding, so I believe I have that correct.

Then when I want to use the data in the object at a latter point I'm trying to do this: // grab the history array

historyArray = [[NSArray alloc] initWithArray:billDetails.historyArray];

Then I'm unarchiving the nsData like this:

  for (NSData *historyData in historyArray)
    {
        // set a instance of the person class to each NSData object found in the temp array
        GD_Owed_HistoryObject *historyObject = [[GD_Owed_HistoryObject alloc] init];
        historyObject = [NSKeyedUnarchiver unarchiveObjectWithData:historyData];
        NSLog(@"This is the history object %@", historyObject);
        // gives me back this: This is the history object <GD_Owed_HistoryObject: 0xb953400>
    }

but this loops seems to just give me back memory locations?

  for (id obj in historyArray){
        NSLog(@"obj: %@", obj);
    }

what it returns: obj: <62706c69 73743030 d4010203 0405081d 1e542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748001 a6090a13 14151655 246e756c 6cd40b0c 0d0e0f10 11125c6f 77656453 61766544 6174655f 10116f77 6564416d 6f756e74 4368616e 6765645f 100f6f77 6564546f 74616c41 6d6f756e 74562463 6c617373 80028003 80048005 5a30352f 31392f32 30313456 2435302e 30305724 3135302e 3030d217 18191c58 24636c61 73736573 5a24636c 6173736e 616d65a2 1a1b5f10 1547445f 4f776564 5f486973 746f7279 4f626a65 6374584e 534f626a 6563745f 10154744 5f4f7765 645f4869 73746f72 794f626a 65637412 000186a0 5f100f4e 534b6579 65644172 63686976 65720008 00110016 001f0028 00320035 003a003c 00430049 0052005f 00730085 008c008e 00900092 0094009f 00a600ae 00b300bc 00c700ca 00e200eb 01030108 00000000 00000201 00000000 0000001f 00000000 00000000 00000000 0000011a>

and when I try to access a property like this it crashes [[historyArray objectAtIndex:indexPath.row] saveDate];

give me this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData saveDate]: unrecognized selector sent to instance 0xb941460'

I guess my question is am I encoding and decoding the object properly?

Edit: So after some more trial and errors I got it working by decoding the object that I saved and then added that object to a new array and using that new array to populate my table view cells.

Thanks for the help!

Upvotes: 0

Views: 1018

Answers (4)

CrimsonChris
CrimsonChris

Reputation: 4641

A better approach than casting everywhere you need to access your object at an index is to add a method to your class like so...

- (HistoryItem *)historyItemAtIndex:(NSUInteger)index {
    return self.billDetails.historyArray[index];
}

then you can do this...

NSString *cellLabelText = [self historyItemAtIndex:indexPath.row].saveDate;

Upvotes: 0

Guferos
Guferos

Reputation: 4367

Use casting to see object's properties

((HistoryObject*)billDetails.historyArray[indexPath.row]).saveData 

Upvotes: 0

Jeferson Assis
Jeferson Assis

Reputation: 125

Is saveDate (nonatomic, strong)?

If weak property and you use ARC, your object will return nil;

NSString *text = [[[billDetails historyArray] objectAtIndex:[indexPath row]] saveDate];

Upvotes: -1

ColdLogic
ColdLogic

Reputation: 7265

You need enclosing brackets to access the object of your historyObject.

NSString *cellLabelText = [[[billDetails historyArray] objectAtIndex:[indexPath row]] saveDate];

Upvotes: 1

Related Questions