PDA

View Full Version : Using NSString to Make a Variable Call


Nick
2004.11.21, 04:18 PM
I apologize for creating this many threads but each has its own topic.

Is there a way that I can use an NSString to call a variable?
ex:

NSPopUpButton *exchangeItem;
int pApples, mApples;
...
NSString *item = [exchangeItem titleOfSelectedItem];
//one such title could be Apples
...
if (...)
{
//add p to the beginning of the item NSString to make pApples
item -= 1;
}
if (...)
{
//add m to the beginning of the item NSString to make mApples
item -=1;
}

I hope that was understandable enough to get a response. Thanks for the help so far.

skyhawk
2004.11.21, 04:20 PM
you want to use an NSMutableString, and to test if it is equal to something use [myString isEqualToString:@"Holybuttmunch"]

OneSadCookie
2004.11.21, 04:25 PM
No, that wasn't intelligible enough to get an answer.

I was left with the impression you wanted an NSDictionary.

Nick
2004.11.21, 04:45 PM
What I was hoping for was to have an NSString contain a string such as "pApples". Then, after creating a variable named pApples, be able to make changes (add, subtract, etc) to the variable by using the NSString. That would help me a lot as there are a lot of objects in my program and to write if statements for all of them would take a while.

skyhawk
2004.11.21, 04:51 PM
What I was hoping for was to have an NSString contain a string such as "pApples". Then, after creating a variable named pApples, be able to make changes (add, subtract, etc) to the variable by using the NSString. That would help me a lot as there are a lot of objects in my program and to write if statements for all of them would take a while.
I think there is a better way to do what you want than what you are trying to do.

Nick
2004.11.21, 05:00 PM
What is the better way? Besides if statements as I will eventually have upwards of 100 items.

blobbo
2004.11.21, 05:08 PM
Sounds like a job for NSDictionary or NSMutableDictionary. Keyed arrays generally do a good job at this...

Otherwise, you should be simply using an NSArray...

Nick
2004.11.21, 07:03 PM
Sounds like I need to hit the books and learn this NSDictionary class. What are the differences between NSDictionary and NSMutableDictionary?

skyhawk
2004.11.21, 07:11 PM
you can change the second one

Shivers
2004.11.21, 07:11 PM
NSDictionary is a keyed array. Quite useful. An NSMutableDictionary is a mutable keyed array. You can add "entries" to it during runtime.

blobbo
2004.11.21, 08:11 PM
Quick explanation:

It's an array, but each element has a name (key) attributed to it instead of just it's position in the array. So you can have an array that looks like this:

Key: Fruit --- Value: Apple
Key: Vegetable --- Value: Cucumber
Key: Herb --- Value: Parsley
Key: Condiment --- Value: Mustard

The docs are good for these basic classes. You need a docs parser - check macupdate.com for one. I use AppKiDo because of it's search support.

Nick
2004.11.21, 08:21 PM
I need a docs parser? For what? Couldn't I just use the dictionaryFromContentsOfFile method?
Is that how I would set up a file with Key: something --- Value: something else?

Steven
2004.11.21, 08:54 PM
He meant a docs parser for reading the documentation, I believe. I've found that the documentation parser from within XCode is quite sufficient despite a bit slow, at least to start with. And yes dictionaryWithContentsOfFile would create a dictionary from a file.

Nick
2004.11.21, 08:57 PM
Cool. I see. I just use the docs parser within Xcode. Works for me.

What would be an example of a file from which I can create a dictionary? I could not find one in the docs.

Josh
2004.11.21, 10:04 PM
PLISTs can be loaded as an NSDictionary.

Nick
2004.11.21, 10:08 PM
I appreciate that. I'll google for it and see what I can find. I have no clue how a PLIST works so I don't know how to write what I want.

Taxxodium
2004.11.22, 02:26 AM
Just load any preferences file into a NSDictionary:

[NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/com.apple.dock.plist" stringByExpandingTildeInPath]];

PS: This is just an example of how to use this. Read the NS(Mutable)Dictionary docs for more info.

Nick
2004.11.22, 10:58 AM
That helps as far as using the PLIST but how do I MAKE the PLIST do/contain what I want?

Taxxodium
2004.11.22, 11:24 AM
int item1, item 2;
NSNumber *item1Object, *item2Object;

item1Object = [NSNumber numberWithInt:item1];
item2Object = [NSNumber numberWithInt:item2];

itemsDict = [[NSMutableDictionary dictionary] retain]; //make sure itemsDict is an ivar

[itemsDict setObject:item1Object forKey:@"item1"];
[itemsDict setObject:item2Object forKey:@"item2"];

//iterate through the keys

NSEnumerator *dictEnum = [itemsDict keyEnumerator];
NSString *key;

while (key = [dictEnum nextObject]) {
if ([key isEqualToString:@"item1"])
something = [itemsDict objectForKey:key]; //something is whatever you wanna do with item 1
}


More info see docs of NSDictionary...

Josh
2004.11.22, 11:26 AM
Are you asking how you save it or how you load it?

Nick
2004.11.22, 11:43 AM
Sorry if I was unclear. I'm asking how to save it.

If I wanted a lot of related things under the same type of category how could I do that? For example, one item is named 'Apples' and under it I would like to keep the knowledge of two variables a buying price and a selling price. Is this possible or would I need two keys 'buyApples' and 'sellApples'?

Taxxodium
2004.11.22, 11:45 AM
Sorry if I was unclear. I'm asking how to save it.

Have you read the documentation? It's in there.

Josh
2004.11.22, 11:47 AM
More specifically, use -[NSDictionary writeToFile:atomically:].

Nick
2004.11.22, 12:29 PM
I guess I understand all of that. I was wondering if there was a way of manually writing this file. Seemed to me to lack point to write code that makes the file that it loads. Why save and load? Could I just open up a new file in Xcode, write some things in (kinda looks like XML) and then just load it in the code? If so, where can I find the structure of the file so that I can make sure the file does what I want it to?

Also (a repost from my last as I added this in with the edit) If I wanted a lot of related things under the same type of category how could I do that? For example, one item is named 'Apples' and under it I would like to keep the knowledge of two variables a buying price and a selling price. Is this possible or would I need two keys 'buyApples' and 'sellApples'?

wadesworld
2004.11.22, 02:13 PM
I guess I understand all of that. I was wondering if there was a way of manually writing this file. Seemed to me to lack point to write code that makes the file that it loads. Why save and load? Could I just open up a new file in Xcode, write some things in (kinda looks like XML) and then just load it in the code? If so, where can I find the structure of the file so that I can make sure the file does what I want it to?

It doesn't kind of look like XML, it is XML.

Use the Property List Editor application to create them. You could also just use a text editor if you're so inclined.


Also (a repost from my last as I added this in with the edit) If I wanted a lot of related things under the same type of category how could I do that? For example, one item is named 'Apples' and under it I would like to keep the knowledge of two variables a buying price and a selling price. Is this possible or would I need two keys 'buyApples' and 'sellApples'?

How about this? (created in Property List Editor):


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Apples</key>
<dict>
<key>Buy Price</key>
<real>2.0</real>
<key>Selling Price</key>
<real>0.5</real>
</dict>
</dict>
</plist>



Keep in mind that this isn't some high-peformance database. It's fine for keeping track of a handful of values. But if you were writing a real-time stock price tracking application for example, this isn't how you'd want to do it - you'd want to some sort of real database.

However, for most casual needs, this is fine.

Wade

Nick
2004.11.22, 03:16 PM
Would these be efficient (or at best just not slow) if dealing with upwards of 100 items like the apple, each containing upwards of 10 keys? Definitely a lot of information to store and I'd rather not try it if somebody already knows it won't work.

Other than that thanks a lot. That was really helpful and I hope to get started with those PLISTs now.

wadesworld
2004.11.22, 03:59 PM
Would these be efficient (or at best just not slow) if dealing with upwards of 100 items like the apple, each containing upwards of 10 keys? Definitely a lot of information to store and I'd rather not try it if somebody already knows it won't work.

Other than that thanks a lot. That was really helpful and I hope to get started with those PLISTs now.

It depends on what your definition of slow is. With 1,000 values, I wouldn't expect it to be particularly fast. It might be OK if you're not going to be constantly updating the values and writing them out to disk.

But the quick answer is - it's not difficult code to write, so why not try it before deciding it won't work and trying to roll your own or interface with some DB? That's the best way to determine if it meets your needs.

Wade

imported_reubert
2004.11.22, 09:09 PM
It wont be slow at all... well it's the kind of thing you want to do at load time, not every frame, but its fast. Have a look at your iTunes plist. ~/Music/iTunes/iTunesMusicLibrary.xml. If you have any number of tracks in iTunes you'll see that plists are quite fast enough.

DoG
2004.11.23, 05:33 AM
plists are loaded into memory, so they are fast. Things start to get slow once it doesn't fit into RAM anymore, but that should not be until your records are in the hundreds of thousands range.

blobbo
2004.11.23, 08:24 AM
Use plists - they're quite easy to manage. And use the builtin NSDictionary load and save methods - they're very easy and, from my experience, very reliable.