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 08-15-2011, 05:04 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 104
Shades of Chaos is on a distinguished road
Default Dynamically create variable name

Hey all,

Is there a way to dynamically create a variable name. Like I pass a string into a method and the method creates an NSString whos name is the string that was passed in. Something like
Code:
-(void)create:(NSString *)str{

	NSString *[NSString stringWithFormat:@"%@", str];
}
Or maybe there is a way to change an already existing variables name.
Code:
NSString *password;
	password.name = entryPassword;
Thanks
Clinton
Shades of Chaos is offline   Reply With Quote
Old 08-15-2011, 05:12 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 Shades of Chaos View Post
Hey all,

Is there a way to dynamically create a variable name. Like I pass a string into a method and the method creates an NSString whos name is the string that was passed in. Something like
Code:
-(void)create:(NSString *)str{

	NSString *[NSString stringWithFormat:@"%@", str];
}
Or maybe there is a way to change an already existing variables name.
Code:
NSString *password;
	password.name = entryPassword;
Thanks
Clinton

In a word, no.

Why do you want to do this? Variable names are compiled away, and by the time your program runs, you can't see them any more (except in the debugger, if you save symbols.)
__________________
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 08-15-2011, 05:15 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 104
Shades of Chaos is on a distinguished road
Default

So that javascript files can pass in a string a create variables in objective-c.
Shades of Chaos is offline   Reply With Quote
Old 08-15-2011, 05:19 PM   #4 (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 Shades of Chaos View Post
So that javascript files can pass in a string a create variables in objective-c.

Objective C is a compiled language. You can't create variables at runtime in a compiled language.

The best you're going to be able to do is to use a dictionary and use the JavaScript variable name as a key to the dictionary. If you want to save numeric values, you'll need to save them as NSNumber objects.

Again, what are you trying to do?
__________________
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 08-15-2011, 05:30 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 104
Shades of Chaos is on a distinguished road
Default

The app has a javascript portion to it and the guy working on the javascript wants to ensure he can make changes and update to the app after I leave. So he wants to be able to send variables to the objective-c code so that they can be saved with NSUserDefaults.
Shades of Chaos is offline   Reply With Quote
Old 08-15-2011, 06:19 PM   #6 (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 Shades of Chaos View Post
The app has a javascript portion to it and the guy working on the javascript wants to ensure he can make changes and update to the app after I leave. So he wants to be able to send variables to the objective-c code so that they can be saved with NSUserDefaults.

You can't create or rename variables dynamically in a compiled language like Objective C (or C, or C++). Not possible. I promise.

If the goal is to save them to user defaults, use a dictionary. That is perfect for this application. You can then either save the dictionary to user defaults as a single object, or copy the objects out of the dictionary into user defaults. Saving the dictionary as an object probably makes more sense because then you can keep the objects added from JavaScript separate from other things your program saves to user defaults.
__________________
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
Reply

Bookmarks

Tags
change, create, dynamic, nsstring, variable

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: 393
13 members and 380 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, dre, fiftysixty, gmarro, jimmyon122, jonathandeknudt, pungs, raymng, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

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