I was wondering how I could access the second NSDictionary in my plist other then the root dictionary, what I would like is to have a string in the second dictionary to show up in the console right now I'm getting all of them. Here's my code, console and plist.
My Plist. (its name is Multi.plist)
My code.
NSString *pathForPlist = [[NSBundle mainBundle] pathForResource:#"Multi" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:pathForPlist];
NSLog(#"%#", [dict objectForKey:#"Abs"]);
And what my console is spitting out right now.
So after looking at that, I'll explain again what I would like, instead of having all of the contents of the Abs NSDictionary being in the console, I would like just to show the title string which lies within the Dictionary. Any help would be greatly appreciated.
Thanks
The Abs entry is a dictionary itself, so all you have to do is :
NSDictionary * abs = dict[#"Abs"];
NSLog(#"%#", abs[#"title"]);
NSDictionary *absDict = [dict objectForKey:#"Abs"]);
NSString *title = [absDict objectForKey:#"title"]);
NSLog(#"Title: %#", title);
Related
I know this has been asked before, but there is no answer that I have found useful.
First off here is my code
// load the .csv file with all information about the track
NSError *error;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"csv" inDirectory:nil];
NSString *datastring1 = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
NSArray *datarow = [datastring1 componentsSeparatedByString:#"\r"];
//fill arrays with the values from .csv file
NSArray *data_seg = [datarow objectAtIndex:0]; //segment number
NSArray *data_slength = [datarow objectAtIndex:1]; //strait length
NSArray *data_slope = [datarow objectAtIndex:2]; //slope
NSArray *data_cradius = [datarow objectAtIndex:3]; //circle radius
NSArray *data_cangle = [datarow objectAtIndex:4]; //circle angle
NSLog(#"%i", [data_seg count]);
Okay, so there is the code, and I read that is has something to do with autorelease, but I was not able to add a retain like NSArray *data_seg = [[datarow objectAtIndex:0] retain]
When I run the code, I get [__NSCFString count]: unrecognized selector sent to instance 0x9d1ad50
Any help is appreciated, I'm not good at programming, and I am very new.
componentsSeparatedByString method returns an NSArray of NSString. Every item that you extract from datarow array is an NSString and an NSString doesn't respond to 'count'. Your code starting at //fill arrays is incorrect. Every objectAtIndex call will return an NSString*.
This is another way of saying that the datatype for data_seg is NSString* (not NSArray*).
With the corrected code snippet, the problem is because data_seg is a string, and -count is not a method of NSString. It seems you think data_seg is an NSArray.
Look at the documentation for -[NSString componentsSeparatedByString:] and see what it returns -- strings! So you get back an array of strings. So what you want is:
NSString *data_seg = [datarow objectAtIndex:0]; //segment number
NSLog(#"my segment number is: %#", data_seg);
I am trying to read and I also need to write to a plist.
So far I am trying to simply read the contents in.
Here is my appSettings.plist:
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:#"appSettings" ofType:#"plist"];
NSDictionary *tmp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
I tried to do this:
NSArray *mruItems = [[NSArray alloc] initWithArray:[tmp objectForKey:#"lastSearches"]];
But it throws an error. A check I did on the [tmp objectForKey:#"lastSearches"] type revealed this is not an NSArray...
How can I read the contents into my NSArray?
Thanks!
(I would love to have some info on writing too)
If you look at the screenshot above you see that it's pretty clear that "lastSearches" is a Dictionary, not a list. You can try to switch the type there to a Array type instead and it should work for you.
Another solution would be to iterate over the keys in that dictionary:
NSDictionary *lastSearches = [tmp objectForKey:#"lastSearches"];
for (NSString *key in lastSearches.allKeys)
{
NSString *value = [lastSearches objectForKey:key];
}
Note that this would not be in order, and you probably would have to sort before iterating over it.
Indeed it's not an array but a dictionary (look at your image).
I'm importing a plist into an NSDictionary object and want to change the contents of the plist once in the dictionary to lowercase. I have tried several different ways with no luck. Maybe I am supposed to edit the strings that are inside the NSDictionary, but then I don't know how to do that.
What I have currently imports the plist correctly, but the lowercaseString line effectively does nothing.
NSString *contents = [[NSBundle mainBundle] pathForResource:#"Assets/test" ofType:#"plist"];
NSString *newContents = [contents lowercaseString];
NSDictionary *someDic = [NSDictionary dictionaryWithContentsOfFile:newContents];
for (NSString *someData in someDic) {
NSLog(#"%# relates to %#", someData, [someDic objectForKey:someData]);
}
I NSLog'd the "content" string and it gave me a path value, so obviously changing that to a lowercaseString wouldn't do anything. I'm feeling like I have to access the strings within the dictionary.
This line
NSString *newContents = [contents lowercaseString];
is change the path string returned from
NSString *contents = [[NSBundle mainBundle] pathForResource:#"Assets/test" ofType:#"plist"];
so you will end up with something like ../assets/test.plist
you will need to walk through the contents of someDic an create a new dictionary based on the old turning strings into lowercase, if you are only concerned about values directly in someDic you can do something like
for( NSString * theKey in someDic )
{
id theObj = [someDic objectForKey:theKey];
if( [theObj isKindOfClass:[NSString class]] )
theObj = [theObj lowercaseString];
[theNewDict setObject:theObj forKey:theKey];
}
Is there simple way to parse the binary plist file into the NSDictionary representation?
I am searching something like that:
NSString* strings = [NSString stringWithContentsOfURL: ... encoding: NSUnicodeStringEncoding error: ...];
NSMutableDictionary* pairs = (NSMutableDictionary*)[strings propertyListFromStringsFileFormat];
Using this code caused the exception while parsing the binary plist file.
Are you looking for [NSDictionary dictionaryWithContentsOfFile:]?
Considering a file called DataStorageFile.plist, you can use:
NSString *dataPath = [[NSBundle mainBundle] pathForResource:#"DataStorageFile" ofType:#"plist"];
self.data = [NSDictionary dictionaryWithContentsOfFile:dataPath];
If you want an array:
self.data = [NSArray arrayWithContentsOfFile:dataPath];
i have a plist which goes like this:
<dict>
<key>11231654</key>
<array>
<string>hello</string>
<string>goodbye</string>
</array>
<key>78978976</key>
<array>
<string>ok</string>
<string>cancel</string>
</array>
i've load the plist using this:
NSString *path = [[NSBundle mainBundle] pathForResource:
#"data" ofType:#"plist"];
// Build the array from the plist
NSMutableArray *array3 = [[NSMutableArray alloc] initWithContentsOfFile:path];
how can i access each element of my plist?
i want to search for a matching key in the plist and than search with its child. how can i do this? any suggestion?
thks in advance!
Arrays are indexed. If you wanted to access the first object in an NSArray, you’d do the following:
id someObject = [array objectAtIndex:0];
If you know the object type, you could do it like this:
NSString *myString = [array objectAtIndex:0];
EDIT: You need to use an NSDictionary for the data that you have—note that it starts with a <dict> tag, not an <array> tag (though there are arrays as values).
NSString *path = [[NSBundle mainBundle] pathForResource:
#"data" ofType:#"plist"];
// Build the array from the plist
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *value = [dict valueForKey:#"11231654"];
Now you can do whatever you want with your array.
Here is a link to a discussion that provided a very good example of how to access your data and which type of storage schemes would be more beneficial under certain circumstances. #Jeff Kelley's solution is on target, I just thought this question would add an additional resource. Hope this helps!
This is how you need to create plist
Set root to array instead of dictionary , this will give you results in sequence.
With below lines of code you can fetch complete plist.
NSString *pathOfPlist = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
arrPlistValue = [[NSArray alloc]initWithContentsOfFile:path];
NSLog(#"Plist Value : %#",arrPlistValue);
After that you can fetch any specific content with below code:
NSString *strName = [[arrTempValue objectAtIndex:0] objectForKey:#"name"];
NSString *strImage = [[arrTempValue objectAtIndex:0] objectForKey:#"image"];
int idValue = [[[arrTempValue objectAtIndex:0] objectForKey:#"id"] integerValue];
BOOL isSubCat = [[[arrTempValue objectAtIndex:0] objectForKey:#"isSubCategory"] boolValue];
With this code you can fetch integer , string , boolean from plist.!