gojohnnyboi
05-14-2008, 06:46 PM
Reading and writing to a .plist file is easy! making it a breeze to save application settings =) this is an example from my "Firmware.app"
- (void)readPlist
{
NSString *filePath = @"/System/Library/CoreServices/SystemVersion.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSString *value;
value = [plistDict objectForKey:@"ProductVersion"];
/* You could now call the string "value" from somewhere to return the value of the string in the .plist specified, for the specified key. */
}
- (void)writeToPlist
{
NSString *filePath = @"/System/Library/CoreServices/SystemVersion.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:@"1.1.1" forKey:@"ProductVersion"];
[plistDict writeToFile:filePath atomically: YES];
/* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
}
Hope this helps some people!
God__
05-15-2008, 05:06 AM
I love the consistency in your code that just proves that you copy and paste most of what you do. Look at the two variable declarations, NSString *variable and NSDictionary* variable1.
It counts for something here.
Anyway, regardless, if all you're trying to do is save your settings, I'm going to suggest that you look into NSUserDefaults. It's a lot easier than reading values (such as floats) and converting them.
You also don't have to worry about where the file is saved or whether or not your application has the appropriate permissions to write to that directory. The OS will automaticalls save the settings in the user's Preferences directory. /var/userName/Library/Preferences/. That way your application can be used by more that one person.
Also, if you are adding and removing settings, you're giong to want to use NSMutableDictionary anyway.
EDIT: You should also add that writing any dictionary to a file will cause the default layout to be a property list formatted in XML. That way people who might read this don't try to create everyline of a file this way.
Either way, thanks for covering the basics. I sure this might help someone.
gojohnnyboi
05-15-2008, 04:55 PM
dude hells no i dont copy and paste. that dont even work in coding...
and sorry i meant to put mutabledict. i have that in firmware.
The Ax
05-19-2008, 10:58 PM
possible to do in JS?
gojohnnyboi
05-20-2008, 03:13 AM
possible to do in JS?
i think u can only change different parts in obj. in jigy u must compioletely rewrite the file
tian_kr
06-07-2008, 05:03 AM
Reading and writing to a .plist file is easy! making it a breeze to save application settings =) this is an example from my "Firmware.app"
- (void)readPlist
{
NSString *filePath = @"/System/Library/CoreServices/SystemVersion.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSString *value;
value = [plistDict objectForKey:@"ProductVersion"];
/* You could now call the string "value" from somewhere to return the value of the string in the .plist specified, for the specified key. */
}
- (void)writeToPlist
{
NSString *filePath = @"/System/Library/CoreServices/SystemVersion.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:@"1.1.1" forKey:@"ProductVersion"];
[plistDict writeToFile:filePath atomically: YES];
/* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
}
Hope this helps some people!
what the heck. could you at least explain something?
i doubt that anyone apart from some devs will understand a shit.
nachos1010
06-07-2008, 05:56 AM
what is a .plist and what does it do?
lgchicken
06-10-2008, 04:05 PM
what the heck. could you at least explain something?
i doubt that anyone apart from some devs will understand a shit.
makes sense to me.
i like gojohnnyboi's little bursts of knowledge, including his serial number thread.
maybe there should be a wiki of useful tips and tricks. (or is there one already?)
gojohnnyboi
06-10-2008, 05:53 PM
makes sense to me.
i like gojohnnyboi's little bursts of knowledge, including his serial number thread.
maybe there should be a wiki of useful tips and tricks. (or is there one already?)
=p thank you! i'm actually working on something like that for my website @_@ shshhhhh
ZunePod
07-14-2009, 03:30 AM
Would this work on something to go in the AppStore.
What I mean is like where the path is defined, what would you change it to to stay in the App's Sandbox?
And another thing, deleting from plists?
Chicken
07-14-2009, 07:49 AM
Bump much?
Pelaez-1
07-14-2009, 09:12 AM
Would this work on something to go in the AppStore.
What I mean is like where the path is defined, what would you change it to to stay in the App's Sandbox?
Use NSUserDefaults
origamitech
01-15-2010, 09:35 AM
how do i read from my array with this?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LocationX</key>
<real>10.0</real>
<key>LocationY</key>
<real>5.0</real>
<key>myArray</key>
<array>
<real>0.0</real>
<real>1.0</real>
<real>22.5</real>
</array>
</dict>
</plist>
how do i read from my array with this?
// ...
NSDictionary *myDictionary = [[NSDictionary alloc] initWithContentsOfFile:@"filePath"];
NSArray *myArray = [myDictionary valueForKey:@"myArray"];
// ...
You can then access the contents of the array as you would normally.
lauNchD
01-15-2010, 10:26 AM
// ...
NSDictionary *myDictionary = [[NSDictionary alloc] initWithContentsOfFile:@"filePath"];
NSArray *myArray = [myDictionary valueForKey:@"myArray"];
// ...
You can then access the contents of the array as you would normally.
Be sure to release the dictionary when you're done! (not @ Axis)
origamitech
01-15-2010, 10:30 AM
That clears it from the RAM, right?
Also how do I do that?
is it this?
[myDictionary release];
That clears it from the RAM, right?
Also how do I do that?
is it this?
[myDictionary release];
Objc uses reference counting for memory management. (Forget about garbage-collection, don't rely on it, and you can't even use it on the iPhone).
An object is destroyed (and its dealloc method called) when its retain count hits zero. When an object is created, assuming it has not received retain calls, it has a retain count of 1. Calling [release] decrements the retain count.
I will not go into terrible detail, as there are many resources on the web, but your purposes, [myDictionary release]; will free the memory.
lauNchD
01-16-2010, 01:57 AM
Just though I'd add this:
When you create an object with a class's "factory method" (you'll see), it will be autoreleased, meaning it will release itself/decrease its retain count at some time after your method gets called and you don't need to.
NSBundle *myBundle = [NSBundle mainBundle]; // shared instance, don't release
NSString *myDictPath = [myBundle pathForResource: @"myDict" ofType: @"plist"];
// ^autoreleased
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile: myDictPath];
NSMutableDictionary *myOtherDict = [NSMutableDictionary dictionaryWithContentsOfFile: myDictPath];
// ^myOtherDict autoreleased
[myDict release];
// I wanna keep myOtherDict though, so I have to retain it before it will 'self-destruct'
myDictInstanceVariable = [myOtherDict retain];
- (void) dealloc
{
[myDictInstanceVariable release];
[super dealloc];
}