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 03-13-2011, 05:32 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
Doodl3 is on a distinguished road
Default Using plists!

Hey, im a newbie when it comes to xcode. So flame all you like for me asking this because we all had to start somewhere, so here we go. Is there anyone who would give a tut on using a plist to say... change the words on a lable when you press a button? I know you can do that by adding what text you'd liek the lable to say in the button method but i wanna learn how to use plists.
Doodl3 is offline   Reply With Quote
Old 03-13-2011, 05:52 PM   #2 (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 Doodl3 View Post
Hey, im a newbie when it comes to xcode. So flame all you like for me asking this because we all had to start somewhere, so here we go. Is there anyone who would give a tut on using a plist to say... change the words on a lable when you press a button? I know you can do that by adding what text you'd liek the lable to say in the button method but i wanna learn how to use plists.
The simplest way to use plists is to create them with one of the writeToFile:atomically: methods.

Say I want to create a plist with an array of strings:


Code:
self.anArray = [NSArray arrayWithObjects: @"one", @"two", @"three"];
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
NSString* filePath = [documentsDir stringByAppendingPathComponent: @"foo.plist"];
[anArray writeToFile: filePath atomically: TRUE];
Then, reading the array from the plist could not be easier:

Code:
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
NSString* filePath = [documentsDir stringByAppendingPathComponent: @"foo.plist"];
self.anArray = [NSArray arrayWithContentsOfFile: filePath];
Once you've read the array from the plist, it's a regular array. You could use it to change the text on a button with code like this:

Code:
-(IBAction) changeButtonText: (id) sender;
{
  int index = arc4random % [self.anArray count];
  NSString* newText = [self.anArray objectAtindex: index];
  ((UIButton*) sender).text = newText;
}
There are probably a couple of minor syntax errors in the code above. I didn't proof it, much less compile and test it. It shows the basic idea.

The code above assumes that it's being used from a single class that has a retained property anArray. You'll need to add code to your object's dealloc method to release the retained property anArray.
__________________
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 03-13-2011, 10:34 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
Doodl3 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
The simplest way to use plists is to create them with one of the writeToFile:atomically: methods.

Say I want to create a plist with an array of strings:


Code:
self.anArray = [NSArray arrayWithObjects: @"one", @"two", @"three"];
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
NSString* filePath = [documentsDir stringByAppendingPathComponent: @"foo.plist"];
[anArray writeToFile: filePath atomically: TRUE];
Then, reading the array from the plist could not be easier:

Code:
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
NSString* filePath = [documentsDir stringByAppendingPathComponent: @"foo.plist"];
self.anArray = [NSArray arrayWithContentsOfFile: filePath];
Once you've read the array from the plist, it's a regular array. You could use it to change the text on a button with code like this:

Code:
-(IBAction) changeButtonText: (id) sender;
{
  int index = arc4random % [self.anArray count];
  NSString* newText = [self.anArray objectAtindex: index];
  ((UIButton*) sender).text = newText;
}
There are probably a couple of minor syntax errors in the code above. I didn't proof it, much less compile and test it. It shows the basic idea.

The code above assumes that it's being used from a single class that has a retained property anArray. You'll need to add code to your object's dealloc method to release the retained property anArray.

Thanks so much, ive read like 50 of your posts now and have learned more in a few hours than in a whole month of programming. All your posts are really great >.< I feel obliged to buy some of your apps. but now, lets see if i can make sense of this and get this to work. Thanks again!
Doodl3 is offline   Reply With Quote
Reply

Bookmarks

Tags
plist, property list

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: 361
9 members and 352 guests
Absentia, akphyo, apatsufas, BinHex, fredidf, iGamesDev, Kirkout, MarkC, mottdog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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