0
votes

In my iPhone App.

Problem: When I am trying to get the value from dictionary in model class it is giving me the nil values.

Why this is happening, is this the memory management issue ?

I did this,

I am passing NSDictionary, from my viewController to NSMutableArray Category and from that I am passing it to my Modelclass.

If you want to see the coding,

  1. In my ViewController.m file

    [arrayNews convertToNewsArticles];

  2. In my NSMutableArray category I am calling method convertToNewsArticles.

    for(NSMutableDictionary *dictionary in self) {

          NewsArticle *newsArticle=[[NewsArticle alloc] initWithDictionary:dictionary];
            [arrayConverted addObject:newsArticle];
    
        }
    

I am talking about this dictionary that I am passing.

  1. Here is my Model class

    -(NewsArticle*)initWithDictionary:dictionary{

    self.title=dictionary[@"title"];
    
    self.author=dictionary[@"author"];
    
    
    self.urlString=dictionary[@"url"];
    
    
    return self;
    

    }

Update:

I solved by using IAModelBase class on GitHub.IAModelBase

2
where is your custom object? - Bista
Why have you subclassed NSMutableArray? You are changing the semantics of initWithArray. This code belongs somewhere else; probably in the object that is fetching the news articles, but if you insist on doing this you just need to say [self addObject:newsArticle] Have you read the Subclassing notes in the NSMutableArray class reference? - Paulw11
I support @Paulw11. Your code design is completly wrong. Also this question has classic XY problem, so please describe what are you trying to achieve not how you try to do it! - Marek R
Custom objects, I made one class, added properties, and added initWithDictionary method ? - Arpit B Parekh
@Paulw11, Marek R, yes I have a doubts about that. So, I hesitate to do. I would asked before that How to design custom objects model classes. Wait for my update. please. - Arpit B Parekh

2 Answers

1
votes

If it is subclass of NSMutableArray then you can add object like,

  [self addObject:newsArticle];

in your for loop and return self will return your final mutable array and you can use it as per desired need!

And refer Apple documentation for more detail. You have to implement some mandatory methods if you want to subclass the NSMutableArray.

0
votes

When I was trying to convert NSDictionary Objects to Model Objects. I could not pass the whole dictionary to Model class. Like mentioned in the question.

But instead I used, IAModel classes

Read the readme file. Also see the 1st Issue,it is solved. Pull Issue