Read Mutable Array from File / Plist - ios

Here is my problem. I show files out of an MutableArray in a UITableView with UISearchBar.
Therefore I made an Array:
allTableData = [[NSMutableArray alloc] initWithObjects:
[[CRFParts alloc] initWithName:#"ABC" andDescription:#"DEF"],
[[CRFParts alloc] initWithName:#"GHI" andDescription:#"JKL"],
nil];
I have round about 2000 lines of this. I build me simple strings out of an excel file but the code inside my .m is huge than. Is there a possibility to read these content out of an file (plist/txt)?
Thanks for helping!

Try looking at This Question. It has a lot of good code on how to read a plist and dictionaries vs arrays.
And next time, search before you ask.

You can easely use:
NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];
or NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];
Check out Apple Documentation on the topic.
Be careful on what structure you are loading as a plist can represent and array or a dictionary at its root. Other thread about it

Related

Can't have access to NSDictionary in NSArray

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.

Comparing NSArray values

So i have 2 arrays, one i have from my local sqlite db and the other i pull down from a server.
When i try to use isEqualToArray: i'll get a NO even though they are similar. This NSHipster article http://nshipster.com/equality/ told me that it's because they compare identity in the memory..? So i'll need some way of only comparing the values?
All help will be appreciated, i've been staring myself blind on this one for quite some time.
So i have 2 arrays:
NSArray *arr1 = [[NSArray alloc] initWithObjects:#"1",#"Hello",#"member",#"Janus", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:#"1",#"Hello",#"member",#"Janus", nil];
When i print them they look exactly the same, when i run NSSet on them like Pablo A suggest i don't get a match and either when i run isEqualToArray on the arrays directly. They are in order and they are always the same number, they are identically but the code won't recognize it.
Sorry, I edit my answer as I realised it may fail. Here you have a better solution:
NSSet *set1 = [NSSet setWithArray:arr1];
NSSet *set2 = [NSSet setWithArray:arr2];
if ([set1 isEqualToSet:set2]) {
// equal
NSLog(#"They contain same objects");
if(arr1.count == arr2.count) {
NSLog(#"They are exactly the same (not checking order)");
}
}
Edited according to comments.

Edit an existing plist in iOS

Good Morning,
I am creating my first iphone app so bear with me.
I have created a plist and have attached a picture so you can see how I set it up.
I'm trying to add another Item under Clients, which has 3 keyvaluepairs in it.
This is what I have so far but I am not understanding how to edit the saved plist I have.
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:#"Property List" ofType:#"plist"];
NSDictionary *clientDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistCatPath];
NSMutableArray *arrayTempClients = [[NSMutableArray alloc] initWithObjects:clientDictionary[#"Clients"], nil];
NSMutableArray *array1 = [[NSMutableArray alloc] initWithArray:arrayTempClients[0]];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict[#"Description"] = #"testAccount";
dict[#"Username"] = #"customUsername";
dict[#"Password"] = #"customPassword";
[array1 addObject:dict];
any help would be greatly appreciated!
You can not make any changes to the app and that includes files in it.
If you need a version of the plist you can edit then on first launch copy the plist file to your app's Document directory. Then you can read it into a NSMutableDictionary, add the new data and save it back.

NSMutableDictionary giving all together same values instead of new fresh values

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

NSMutableArray getting changed

I have an NSMutableArray (detailsMA)having 100 strings like #"True".
Am storing that array in NSMutableDictionary which is already allocated.
NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA];
[detailsDictionary setObject: array1 forKey:#"detailsMA"];
Now i changed the values of detailsMA, like replacing strings to #"False", After that am retrieving the original array values,
[detailsMA removeAllObjects];
[detailsMA addObjectsFromArray:[detailsDictionary objectForKey:#"detailsMA"]];
Here am getting modified values, not the original values.
I need original values.
Please help me.
You should copy the array.
NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA copyItems:YES];
Don't forget to implement the NSCopying protocol in the classes for the objects to be copied
EDIT:
When you add the objects again, make a copy before adding.
[detailsMA addObjectsFromArray:[[NSArray alloc] initWithArray:[detailsDictionary objectForKey:#"detailsMA"] copyItems:YES]];

Resources