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 11-08-2008, 07:22 AM   #1 (permalink)
shwakaka! yeah!
 
Join Date: Oct 2008
Location: whistler, ca
Posts: 17
adanv1 is on a distinguished road
Red face What's Flash's set("var"+someNumber, 5) translated into cocoa?

Easy answer needed:

What's Flash's set("_root.var"+someNumber, 5) translated into cocoa?

Basically,
I'm looking for a way to dynamically set an instance name in the following:

Code:
	myImageView.image = bgImg;
to something like
Code:
set("myImage"+someNumber+"View").image = bgImg;
where someNumber is a string.

Been searching google and not turning up anything in cocoa.
adanv1 is offline   Reply With Quote
Old 11-08-2008, 08:35 AM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 123
dicklacara is on a distinguished road
Default

Quote:
Originally Posted by adanv1 View Post
Easy answer needed:

What's Flash's set("_root.var"+someNumber, 5) translated into cocoa?

Basically,
I'm looking for a way to dynamically set an instance name in the following:

Code:
	myImageView.image = bgImg;
to something like
Code:
set("myImage"+someNumber+"View").image = bgImg;
where someNumber is a string.

Been searching google and not turning up anything in cocoa.

[someString appendString:anotherString];

You might want to review the API for NSString. NSString includes methods to do most anything you want with strings (except RegExp).

HTH

Dick
dicklacara is offline   Reply With Quote
Old 11-08-2008, 11:58 PM   #3 (permalink)
shwakaka! yeah!
 
Join Date: Oct 2008
Location: whistler, ca
Posts: 17
adanv1 is on a distinguished road
Default

I guess I should clarify a bit more.

I'm just looking to do something along the lines of a dynamic variable name:

Code:
//declared the bgImage above to be a UIImage
// i is either an integer or a nsstring, basically a number
while(i<100){

set("myImage"+i+"View").image = bgImg;
[bgImg release]
//add to i and continue
}
hasn't anyone done this in their program rather than write out 100 different myImage1View, myImage2View, myImage3View etc? if so, how? I know it's got to be one easy thing I'm completely overlooking.
adanv1 is offline   Reply With Quote
Old 11-09-2008, 11:26 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default Dynamic variable adressing

Your looking for accessing variables by there name, have you tried just putting the values in an array?

I guess what you want is some sort of Eval() method.
QuantumDoja is offline   Reply With Quote
Old 11-09-2008, 12:08 PM   #5 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 123
dicklacara is on a distinguished road
Default

Quote:
Originally Posted by QuantumDoja View Post
Your looking for accessing variables by there name, have you tried just putting the values in an array?

I guess what you want is some sort of Eval() method.
Yeah, while dynamic variable names are fairly common (and useful) in interpreted languages, I don't know how you'd implement them in a compiled language like Obj-C.

I suppose you could do some kind of pointer manipulation-- but that would get rather abstract and difficult to implement, understand, debugg and maintain.

Another suggestion would be to use an NSDictionary of key-value pairs. The Key would be the variable name and the value would be the variable's content.

If the variables were named myVar1...myVarn it would be pretty easy to iterate over the dictionary.

HTH

Dick
dicklacara is offline   Reply With Quote
Old 11-09-2008, 12:14 PM   #6 (permalink)
shwakaka! yeah!
 
Join Date: Oct 2008
Location: whistler, ca
Posts: 17
adanv1 is on a distinguished road
Default

wow. I never knew the languages were so different when it comes to certain characteristics.

I guess the easiest way here is just to go manually with a bunch of if statements in my case, just have 32 different image spots.
adanv1 is offline   Reply With Quote
Old 11-09-2008, 12:31 PM   #7 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 123
dicklacara is on a distinguished road
Default

Quote:
Originally Posted by adanv1 View Post
wow. I never knew the languages were so different when it comes to certain characteristics.

I guess the easiest way here is just to go manually with a bunch of if statements in my case, just have 32 different image spots.
Before you spend the time creating 32 sets of duplicate code (and later maintaining them)...

...Have a look at NSArray and NSDictionary. It is easy to create an array where the value of myVar7 is the 7th entry in an array of values (objects).

With NSDictionary there are essentially 2 arrays: one of names and the other of values. So, if your variable names are not incrementally named and/or in no particular order, you can say: From the array of variables, give me the value for the variable named "Bo-Diddley".

I think you will find it a lot easier to code and maintain.

HTH

Dick
dicklacara is offline   Reply With Quote
Old 01-22-2009, 02:46 AM   #8 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 12
fsdn is on a distinguished road
Default

But is there a real solution for those who really need eval-equivalent for building helpers, factories or anything else ?

My variables are instance attributes for exemple and that whould be ugly to stock all instance's variables into a dictionnary...

Is it possible to change a variable we didn't know the name before compilation or not ?
fsdn is offline   Reply With Quote
Old 01-22-2009, 10:43 AM   #9 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
PhoneyDeveloper is on a distinguished road
Default

It is possible to set variables by name. Read the Key Value Coding Programming Guide.
PhoneyDeveloper is offline   Reply With Quote
Old 01-22-2009, 11:00 AM   #10 (permalink)
iPhone Developer
 
Join Date: Sep 2008
Location: Canada
Posts: 159
Henning is on a distinguished road
Default

NSString *imageName = [NSString stringWithFormat:@"myImage%dView", i];

Maybe I'm misunderstanding something?

Edit: Wait, I get it. Sorry. Please disregard.

Last edited by Henning; 01-22-2009 at 11:03 AM.
Henning is offline   Reply With Quote
Old 01-22-2009, 12:03 PM   #11 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 12
fsdn is on a distinguished road
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
It is possible to set variables by name. Read the Key Value Coding Programming Guide.
Thanks a lot !!! Exactly what I was looking for !!!
fsdn 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: 325
11 members and 314 guests
condor304, Desert Diva, Domele, dre, dreamdash3, mottdog, oceanlablight, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,659
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dreamdash3
Powered by vBadvanced CMPS v3.1.0

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