-1

I'm creating this loop, and as you can see, there is a problem: you can't initialize a variable-sized object in this kind of situation. How would I set the loop up so that it creates pointer names for the UITouch unique, without an array, for example if there are 3 touches, the loop would create three separate UITouch pointers, touch1, touch2, and touch3.

for (int i = 1; i <= touchCount; i++) {

    UITouch *touch[i] = [touchArray objectAtIndex:i-1];
}
7
  • possible duplicate of Object name from String in Objective-C Commented Jun 10, 2011 at 5:57
  • Simple answer is you can't. What do you need the touches for. That might help us give you the proper answer. Commented Jun 10, 2011 at 5:57
  • 1
    If the scope of the objects are only inside the for loop, What is the need for giving them different names? Commented Jun 10, 2011 at 5:57
  • 1
    I am not seeing why a "unique name" is needed. What is wrong with an array of n (3 in this case) elements? Commented Jun 10, 2011 at 5:59
  • 1
    @Conor Taylor, But you are creating the objects inside the for loop, isn't it? How do you think you can use those objects outside the loop? :-) I guess there is little confusion in what you are trying to do Commented Jun 10, 2011 at 6:09

1 Answer 1

2

What you want to do is unnecessary since you already have pointers to your touches stored in touchArray. If you want to explicitly create distinct object for each touch you have to create them manually.

Or you could do a little trick like this:

//Create a method named touchNumbered:(int)number

-(UITouch*)touchNumbered:(int)number{
   return [touchArray objectAtIndex:number];
}

It all depends on what you want to do with those touches.

Sign up to request clarification or add additional context in comments.

Comments

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.