0

I'm creating an application, in which i used an NSMutableArray to stock some objects. To do this, no problem in first look :

ArrayOfViews = [[NSMutableArray alloc] init];
[ArrayOfViews addObject:...];

But my object are UIViews, that i create. For example, I have a file named "Level1". How could I add an object from "Level1", like :

Level1 * level1view;

decalered in the same UIViewcontroller from my NSMuttableArray ?

Like a sort of :

for (i = 0, i < max, i++)
{
    [ArrayOfViews addObject:[objectWithName:[NSString stringWithFormat:@"level%iview", i]]];
}

I don't know how could I wrote it with good encoding.

Second, to use the objects with a selector and parameters, how could i do ?

Because i tried :

NSString * futureSelector = [NSString stringWithFormat:@"Level%iappearswithTime:", number];
SEL s = NSSelectorFromString(futureSelector);
NSInvocation * invoc = [[NSInvocation alloc] init];
[invoc setSelector:s];
[invoc setArgument:&t atIndex:1];
[invoc setTarget:[ArrayOfViews objectAtIndex:number]];
[invoc invoke];

To replace this sort of code :

[level1view Level1appearswithTime:t];

where variable t is an NSTimeInterval

Thanks for your help !

1 Answer 1

1

Since variable names are gone during compilation, you essentially can't do this (unless, of course, the variables are instance variables in which case their name is preserved, but then you still don't want to do it, I'm sure.)

As to the selector-and-string-problem: why not use NSStringFromSelector(), NSSelectorFromString() and - [NSObject performSelector:withObject:]?

SEL s = NSSelectorFromString([selectorsArray objectAtIndex:0]);
[someObject performSelector:s withObject:42];
7
  • I can't do this, i've tried, because when i test this : [[ArrayOfViews objectAtIndex:number] performSelector:s withObject:t]; it gives me : Sending 'NSTimeInterval' (aka 'double') to parameter of incompatible type 'id' (where you put 42) Commented Jul 7, 2013 at 16:51
  • @user2057209 Then 1. turn off stupid ARC, 2. google NSInvocation. It rocks too, you can specify all sorts of types. Commented Jul 7, 2013 at 16:58
  • I've edited with NSInvocation (see first post), but it doesn't looks like working. Commented Jul 7, 2013 at 17:21
  • @user2057209 What exactly does "it doesn't look like working" mean in your case? Commented Jul 7, 2013 at 17:36
  • That the object [ArrayOfViews objectAtIndex:number] calls the dynamic selector (create with a string) and one parameter. Commented Jul 7, 2013 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.