开发者

dynamic Instance naming

开发者 https://www.devze.com 2023-04-05 05:26 出处:网络
I\'m developing an iPad App and need some help. Through a button within my App I want to create one object at a time.

I'm developing an iPad App and need some help.

Through a button within my App I want to create one object at a time. So every time the button is touched one object should be created.

The problem I have is: I want to assign each object a dynamic name to identify this object. This would be something like: form0, form1, form2, ..., formN.

This Name corresponds to an instance variable within every object. So the form1 instance has a number attribute which is 1.

But how do I assign this form1, form2, etc. to a new instance?

I tried to initialize a new instance with the return of a method which creates the formX-String:

-(NSString*)giveMeName{
NSString* simpleName = @"form";
NSString* newName = [simpleName stringByAppendingString:[NSString stringWithFormat:@"%d", questionCounter]];
return newName;

}

where questionCounter is a variable which holds the int identifier for both formX and the instance number attribute.

But when I wan开发者_StackOverflowt to initialize a new instance with this function as name it's not working:

TSForm* [self giveMeName] = [[TSForm alloc] initWithInt:questionCounter headline:headlineText intro:introText];

Obviously I got something wrong with the inner working of Objective-C. Please help me out.


what you're trying to do isn't really possible. One way that you could achieve the affect you're looking for is using an NSDictionary. For every TSForm object you create, you add that object to the dictionary with the key of the giveMeName return value.

So you start by creating your dictionary:

NSMutableDictionary *formDict = [[NSMutableDictionary alloc] init];

Then, every time you create an object, add it to the dictionary:

id *newTSForm = [[TSForm alloc] init]; // Or however you create a TSForm
[formDict setObject:newTSForm forKey:[newTSForm giveMeName]];

Then when you want to pull out the form you're looking for, you just ask the dictionary based on the name you provided:

 [formDict valueForKey:nameOfForm]; // nameOfForm is the name provided by giveMeName

Hope this helps!


use NSMutableArray and keep adding your items there.


Even if what you are trying to do is technically possible, that's using tricsk in low-level objective-C runtime and KVC stuff and so on for nothing.

Using a simple NSMutableArray to keep track of all you instances (and using the index in the array to know which form you are dealing with) is the way to go.

I don't think you really need your unique identifier stuff for that (if so, you are probably thinking about your project the wrong way), as long as you have a way in your code to differentiate each form and manipulate them (the first form created will then be at index 0, the second at index 1… of your NSMutableDictionary)

If you really need this special unique identifier anyway for some strange reason, you can still use an NSMutableDictionary and use the unique identifier as your key of your dict and the form as the associated value. But you should probably think twice about your architecture ad the real need for this before, as it seems quite strange app architecture/design to do so based on your description of your needs in your question.


What you are looking for is some kind of variable variable, which don't really exist in objective-C.

This question (Objective C Equivalent of PHP's “Variable Variables”) has some different suggestions for getting similar results.

0

精彩评论

暂无评论...
验证码 换一张
取 消