i am constructing All data to look like a response data dictionary from a server.
Now, newsFeedsDict1 which should Dictionary for both Bolly and Global is not only showing all data inside Global dictionary only. While my for loop is running its showing correct data for Bolly. but for 2nd time its showing Bolly's data also in Global dictionary.
if(internetStatus == NotReachable)
{
NSMutableArray *titleArr = [[NSMutableArray alloc] init];
NSMutableArray *wholeFeeds = [[[NSMutableArray alloc] init] autorelease];
[titleArr addObject:#"Bolly"];
[titleArr addObject:#"Global"];
for (NSString *title in titleArr) {
//titleArr = [[NSUserDefaults standardUserDefaults] objectForKey:#"TitleArray"];
NSLog(#"TITle arr %#",titleArr);
NSLog(#"No internet");
OrderedDictionary *newsFeedsDict1 = [[[OrderedDictionary alloc] init] autorelease];
NSMutableDictionary *newsFeedsDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
NSMutableDictionary *articleDict = [[[NSMutableDictionary alloc] init] autorelease];
[articleDict setObject:myLocalArray forKey:#"article"];
[newsFeedsDict setObject:articleDict forKey:#"Articles"];
[newsFeedsDict setObject:title forKey:#"#name"];
[newsFeedsDict1 setObject:newsFeedsDict forKey:title];
[wholeFeeds addObject:newsFeedsDict1];
NSLog(#"news feed dict %#",newsFeedsDict1);
NSMutableDictionary *temparticleDictionary = [[NSMutableDictionary alloc] init];
self.articleDictionary = temparticleDictionary;
self.categoriesDictionary = [[NSMutableDictionary alloc] init];
self.categoriesDictionary =newsFeedsDict1;
[self createArticleDictionaryForCategory:newsFeedsDict];
}
}
It's difficult to tell what your code is supposed to do, and how you can tell that one dictionary has the same content as another. A couple problems:
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
The first line is entirely unnecessary. You're creating a new array, assigning it to myLocalArray, and then assigning something else to myLocalArray. You do the same later with self.categoriesDictionary. This leads me to believe that you have some misunderstanding with respect to object pointers.
So, the array that you get from your shared database ends up at myLocalArray, and you then add that array to the dictionary at articleDict, and then add articleDict to newsFeedDict and in turn add that to newsFeedDict1. And then you log newsFeedDict1. You do exactly the same for both your titles, #"Bolly" and #"Global", so it's not at all surprising that you see the same output in both cases.
I'm having a hard time seeing why you expect the output to be different, and I have to guess that again it's due to a misunderstanding of what happens when you assign one object pointer to another. Or, perhaps you're expecting the array that you get from [[Database sharedDatabase] getArticleData:title]; to be different because you're passing in different titles. Maybe you really should be getting different arrays there; it would be a good idea to look at what happens in that -getArticleData: method and whether it really is giving you the right results for each title.
If that doesn't help, take some time to clean up your code so that it's easier for us, and more importantly, for you to really see what's going on. Also, please edit your question to give a better description of what you're seeing and how that's different from what you expect to see.
Can you write the snippet for *getArticleData()* method
Related
I have to assign countries, states & cities getting in response from JSON, in an NSMutableArray, Which is initialized in Modal Class.
I will have to remove all objects in order to set new states and cities, doing that crashes with error
incorrect checksum for freed object - object was probably modified
after being freed.
*** set a breakpoint in malloc_error_break to debug
then in answer https://stackoverflow.com/a/12050676/5568169 came to know that assigning nil to mutableArray will work, and it worked, But now User can again select another country, So now allocating memory [myMutableArray alloc] init]], gives me the same error i was getting in starting.
-(void)fetchStates:(NSString*)idString {
[registrationModalContactVC.allStateArray removeAllObjects];
registrationModalContactVC.allStateArray = nil;
[registrationModalContactVC.allStateDict removeAllObjects];
registrationModalContactVC.allStateDict = nil;
registrationModalContactVC.allStateArray = [NSMutableArray new];
registrationModalContactVC.allStateDict = [NSMutableDictionary new];
}
Kindly help
You should not be dong this :
[myMutableArray alloc] init]
What you mean to do is :
myMutableArray = [[NSMutableArray alloc] init];
i think your mutablearray is a datasource of a pickview , when you remove all objects, how is your pickview's status.
Removing below code is working, but it may give the same error again
registrationModalContactVC.allStateArray = nil;
registrationModalContactVC.allStateDict = nil;
registrationModalContactVC.allStateArray = [NSMutableArray new];
registrationModalContactVC.allStateDict = [NSMutableDictionary new];
I have a very strange behaviour with NSArray.
Firstly, i parsed an XML file and return a valid NSArray with NSDictionary inside:
NSArray *arrayOfDict = [parser parseWithXMLFile:filename];
In debugger it's fully valid. Then, i want to proccess all dictionaries in this array:
NSMutableArray* arrayOfProducts = [[NSMutableArray alloc] init];
for (NSDictionary* dict in arrayOfDict) {
Product* product = [[Product alloc] initWithName:dict[#"name"]
type:dict[#"type"]
imageName:dict[#"image"]
description:dict[#"description"]];
[arrayOfProducts addObject:product];
[product release];
}
And in this loop is a problem: a dict variable has value nil. And i don't know what to do with this. In debugger i evaluate value of arrayOfDict[someIndex] and get a right value, but in the programm itself it doesn't work.
May be it's the problem with MRR, i don't feel myself confidenly while using MRR and there is a mistake of using it.
P.S. I know that using MRR is stupid today, but in this project i must use it.
I have four pieces of data I always want to keep together: 2 NSArrays and 2 ints. I thought a struct might be a good idea, but I get the "ARC does not allow objects in structs" error. What would be the best way to encapsulate the data? Using an NSDictionary?
Create a class with 4 properties. This allows for future growth by adding standard methods like isEqual: and making it work with NSCoding, etc. You can also add convenience constructors and other helpful methods as needed.
You can do it this way:
NSArray *arr1 = [[NSArray alloc] initWithObjects:#"apple",#"orange", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:#"pine",#"pinnacle", nil];
NSInteger val1 = 1;
NSInteger val2 = 2;
NSMutableDictionary *dictTest = [[NSMutableDictionary alloc] init];
[dictTest setObject:arr1 forKey:#"arr1"];
[dictTest setObject:arr2 forKey:#"arr2"];
[dictTest setObject:[NSNumber numberWithInteger:val1] forKey:#"val1"];
[dictTest setObject:[NSNumber numberWithInteger:val2] forKey:#"val2"];
arrayOfElements = [NSMutableArray arrayWithArray:[someObj getArray]];
and
arrayOfElements = [[NSMutableArray alloc] init];
arrayOfElements = [someObj getArray];
What's the difference?
The first arrayOfElements does not seem to lose its objects when it returns count in numberOfRowsInSection:(NSInteger)section, but the second one does. I get EXC_BAD_ACCESS when I do it the second way.
EDIT:
Can I suppose now that this is the best way,
arrayOfElements = [[NSMutableArray alloc] initWithArray:[someObj getArray]];
because I am initializing an array with the contents of whatever will be autorelease'd, and I now have a fully independent array in the current class, that is viewDidLoad, oops sorry, ViewController.
This line creates an NSMutableArray from an existing array
arrayOfElements = [NSMutableArray arrayWithArray:[someObj getArray]];
This combination first creates an NSMutableArray and then instantly discards it replacing it with what is returned by [someObj getArray]
arrayOfElements = [[NSMutableArray alloc] init]; // Create new NSMutableArray
arrayOfElements = [someObj getArray]; // Throw away the newly created array and replace with the result of [someObj getArray]
If you are not using ARC then it is purely by luck that either would work.
In both cases arrayOfElements is being assigned an autorelease'd object - which will be cleared soon (most likely the next runloop). It is only by chance that nothing else has been written over this point of memory which allows one of your implementations to still work.
If you are not using ARC then really you should update your project to be using it will handle a lot of cases like this for you.
You should definitely be using properties (not bare ivars) as this will help reduce memory issues (for non-ARC) and give a more consistent interface to your code.
In your header (or class extension) declare the property like this
#property (nonatomic, strong) NSMutableArray *arrayOfElements;
Now for ARC you can simple do
[self setArrayOfElements:[[someObj getArray] mutableCopy];
for non-ARC you can do
NSMutableArray *array = [[someObj getArray] mutableCopy];
[self setArrayOfElements:array];
[array release]; array = nil;
Also note that getArray is a bad method name.
The use of “get” is unnecessary, unless one or more values are returned indirectly.
Coding Guidelines
When you are adding objects to mutable array from another array, try this:
[arrayOfElements addObjectsFromArray: [someObj getArray]];
If you're not using ARC, you need to make sure its retained.
if (arrayOfElements) {
[arrayOfElements release];
}
arrayOfElements = [[NSMutableArray alloc] initWithArray:[someObj getArray]];
I am from android background and I just started working on iPhone
I want to perform this operation in iPhone as I do in Android.
ArrayList<String> aa = new ArrayList<String>();
public void fillArray(String s)
{
aa.add(s);
}
As Binyamin Sharet suggest you have to use NSMutableArray. This class allows you to create a dynamic array. You can perform addition or deletion. On the contrary NSArray is a immutable version of it. You cannot add or delete objects to a NSArray once created.
The same distinction can be applied to NSDictionary and NSMutableDictionary (and other).
Here a simple example.
NSMutableArray* arr = [[NSMutableArray alloc] init];
[arr addObject:#"first string"];
[arr addObject:#"second string"];
An important aspect of NSMutableArray (the same could be applied to other class) is the memory management one. When you add an object to a NSMutableArray it retains objects added to it. So, if you NOT use ARC you have to deal with this aspect.
NSMutableArray* arr = [[NSMutableArray alloc] init];
id obj = [[NSObject alloc] init]; // a general object
[arr addObject:obj];
[obj release];
For further info I suggest you to read about NSMutableArray class reference.
Hope it helps.