Search code examples
objective-cnsdictionarynsobject

How to initialise a NSObject Class


I am trying to pass a NSDictionary object over to my NSObject Class, so that I can use the object elements in my current viewController however I am having some trouble initialing it.

This is what my NSObject class Method header looks like

NSObjectClass.m

- (GetSeriesDictionary *)assignSeriesDictionaryData:(NSMutableDictionary*)seriesDictionaryData {

    // Initialize values
    seriesID = [[seriesDictionaryData valueForKey:@"SeriesID"] integerValue];

    seriesType = [[seriesDictionaryData valueForKey:@"SeriesType"] integerValue];

//...

Then inside the view controller I am trying to use this NSObject like this

ViewController.m

// inside random method
    GetSeriesDictionary *getSeriesObj = [[GetSeriesSeriesDictionary alloc] init];
    getSeriesObj = (GetSeriesSeriesDictionary *)series; // this is where I am having the problem

    NSLog(@"%i", getSeriesObj.seriesID);
//..

But then I am reviving this error

-[__NSDictionaryM seriesID]: unrecognized selector sent to instance 0x1e8de940

So I would like some help with initialing my NSObject so that I can call the elements out of the object like

getSeriesObj.seriesID

Solution

  • You cannot just cast the objects. You need to call assignSeriesDictionaryData: that you've created.

    // inside random method
    GetSeriesDictionary *getSeriesObj = [[GetSeriesSeriesDictionary alloc] init];
    [getSeriesObj assignSeriesDictionaryData:series]; 
    
    NSLog(@"%i", getSeriesObj.seriesID);
    //..
    

    Also, as you may have noted, I'd not have a NSObjectClass. That mapping fits better on GetSeriesDictionary itself (you can create a custom init that does that).