Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2011, 07:44 PM   #1 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default Quick Question About .plist data

I have a plist that looks like the following:



To set the label to exercsieName, I am using the following code:
Code:
NSMutableArray *exerciseDetailArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"InfoArray"] mutableCopy];
 int indexValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"indexVal"] intValue];    
 self.name.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exerciseName"];
 [exerciseDetailArray release];
I want to also set a label to muscleName, I tried adding the following but it didn't work.
Code:
self.muscle.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"muscleName"];
Any suggestions?
cityofangels is offline   Reply With Quote
Old 05-26-2011, 07:58 PM   #2 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

I tried this, but it isn't working properly either:

Code:
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
    self.muscleArray = rootLevel;
    self.muscle.text = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"muscleName"];
    [rootLevel release];

Last edited by cityofangels; 05-26-2011 at 08:07 PM.
cityofangels is offline   Reply With Quote
Old 05-26-2011, 09:20 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by cityofangels View Post
I have a plist that looks like the following:



To set the label to exercsieName, I am using the following code:
Code:
NSMutableArray *exerciseDetailArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"InfoArray"] mutableCopy];
 int indexValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"indexVal"] intValue];    
 self.name.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exerciseName"];
 [exerciseDetailArray release];
I want to also set a label to muscleName, I tried adding the following but it didn't work.
Code:
self.muscle.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"muscleName"];
Any suggestions?
I'm confused. You posted data from the middle of a plist. It shows an entry, item 2, from the middle of an array. It isn't clear what is the outermost object in that plist.

Then you posted code that's reading data from NSUserDefaults, with a key of "InfoArray". Is what you posted one entry from the "InfoArray" structure you're reading from user defaults?

What you showed is this:

Array entry 2
--Dictionary
----Key "exercises"
------ array
----------item 0
------------ dictionary
-------------- key "exerciseName"
---------------- string "Balance Board"
----Key "muscleName"
------ string "Calves"

Your code never reads the "exercises" key from the outermost dictionary array entry 2, so I don't understand how the first block of code works.

The second block of code is written as if the same dictionary that contains the "exerciseName" key also contains a "muscleName" key, but it does not. From the little image you posted, I can't tell for sure what the structure of your file is. It looks like the outermost dictionary contains the key/value "exercises" and the key/value "muscleName". Thus, you need to fetch the "muscleName" key/value from the same dictionary you used to read your "exercises" key/value pair. But, as I said,you did not show how you're reading the "exercises" key/value.

You need to show us a more complete picture of what's going on.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 05-26-2011 at 09:41 PM.
Duncan C is offline   Reply With Quote
Old 05-26-2011, 09:25 PM   #4 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
I'm confused. You posted data from the middle of a plist. It shows an entry, item 2, from the middle of an array. It isn't clear what is the outermost object in that plist.

Then you posted code that's reading data from NSUserDefaults, with a key of "InfoArray". Is what you posted one entry from the "InfoArray" structure you're reading from user defaults?

What you showed is this:

Array entry 2
Dictionary
Key "exercises"
array
item 0
dictionary
key "exerciseName"
string "Balance Board"
Key "muscleName"
string "Calves"

Your code never reads the "exercises" key from the outermost dictionary array entry 2, so I don't understand how the first block of code works.

The second block of code is written as if the same dictionary that contains the "exerciseName" key also contains a "muscleName" key, but it does not. From the little image you posted, I can't tell for sure what the structure of your file is. It looks like the outermost dictionary contains the key/value "exercises" and the key/value "muscleName". Thus, you need to fetch the "muscleName" key/value from the same dictionary you used to read your "exercises" key/value pair. But, as I said,you did not show how you're reading the "exercises" key/value.

You need to show us a more complete picture of what's going on.
Thanks,

Here is a full picture that should show the structure of the .plist

Both methods I had are loading data from the same plist. I used the first method to successfully update a label with exercise name. I also used the second method to successfully create muscleArray elsewhere to load a tableView.

What I need to do is use that muscleArray and load the label with the muscleName depending on what muscle from the tableView was selected.
cityofangels is offline   Reply With Quote
Old 05-26-2011, 09:51 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by cityofangels View Post
Thanks,

Here is a full picture that should show the structure of the .plist

Both methods I had are loading data from the same plist. I used the first method to successfully update a label with exercise name. I also used the second method to successfully create muscleArray elsewhere to load a tableView.

What I need to do is use that muscleArray and load the label with the muscleName depending on what muscle from the tableView was selected.
To repeat:

The second block of code is written as if the same dictionary that contains the "exerciseName" key also contains a "muscleName" key, but it does not.


The first code you posted that reads "exerciseName" gets an object from an array, and assumes it's a dictionary. It then looks for the key "exerciseName" in that dictionary.

Your second block of code tries to read another key "muscleName" from the same dictionary. There is no key "muscleName" in that dictionary. Instead, "muscleName" is is a key in a parent dictionary. That's why it doesn't work. You need to look at your data carefully, and pay attention to the grouping of your objects. Either the data is not structured correctly, or your code is not written to read the structure correctly. I don't have a clear enough understanding to tell which. It's up to you to figure it out.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-26-2011, 10:43 PM   #6 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
To repeat:

The second block of code is written as if the same dictionary that contains the "exerciseName" key also contains a "muscleName" key, but it does not.


The first code you posted that reads "exerciseName" gets an object from an array, and assumes it's a dictionary. It then looks for the key "exerciseName" in that dictionary.

Your second block of code tries to read another key "muscleName" from the same dictionary. There is no key "muscleName" in that dictionary. Instead, "muscleName" is is a key in a parent dictionary. That's why it doesn't work. You need to look at your data carefully, and pay attention to the grouping of your objects. Either the data is not structured correctly, or your code is not written to read the structure correctly. I don't have a clear enough understanding to tell which. It's up to you to figure it out.
Thanks Duncan.

So my data is structured correctly, but my code is not written to read the structure correctly. So how can I edit the method to reflect this?

Each exerciseName's parent has a muscleName.
cityofangels is offline   Reply With Quote
Old 05-27-2011, 06:47 AM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by cityofangels View Post
Thanks Duncan.

So my data is structured correctly, but my code is not written to read the structure correctly. So how can I edit the method to reflect this?

Each exerciseName's parent has a muscleName.
I pointed you at the problem, now you need to think it through for yourself.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-27-2011, 12:34 PM   #8 (permalink)
Beginner iOS Dev
 
Join Date: Feb 2011
Location: Boston
Posts: 372
cityofangels is on a distinguished road
Default

I tried this but no luck either. It works elsewhere in my code to load a tableView with muscleName items.

Code:
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.muscleArray = rootLevel;
rootLevel release];
label.title = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"muscleName"];
I don't understand why it works there but won't work here to update the label.

Last edited by cityofangels; 05-27-2011 at 12:38 PM.
cityofangels is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 342
6 members and 336 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0