3

I'm trying to set a variable on a view that im getting ready to load. Heres the code for setting it:

 NSInteger amountOfQuestions = [self.questions.text intValue];
    Timer_ViewController *tvc = [[Timer_ViewController alloc] initWithNibName:@"Timer_ViewController" bundle:nil];

    tvc.amountOfQuestions = &amountOfQuestions;

And here's the @interface for Timer_ViewController:

@interface Timer_ViewController : UIViewController
{
    NSInteger amountOfQuestions;
}

    @property (nonatomic) NSInteger *amountOfQuestions;



@end

Im relatively new to objective-c so pointing out of obvious design flaws would be appreciated.

Jonah Katz
  • 5,230
  • 16
  • 67
  • 90

1 Answers1

9

You shouldn't make NSInteger a pointer - it's not an object type, just a typedef of a plain primitive type. If you're using a relatively recent version of Xcode, you can go with something like this:

@interface Timer_ViewController : UIViewController
@property (nonatomic, assign) NSInteger amountOfQuestions;
@end

And set it using:

tvc.amountOfQuestions = [self.questions.text intValue];

You don't even have to declare the instance variable or @synthesize the property - Xcode will handle it for you.

Tim
  • 59,527
  • 19
  • 156
  • 165
  • That gives me the error/warning "incompatible integer to variable conversion..' – Jonah Katz Sep 14 '12 at 00:12
  • Always omit "assign". It serves no purpose. – SmileBot Nov 15 '14 at 20:14
  • With respect, that's a style preference. To me, explicitly including `assign` in the property declaration reminds me – without having to think, even briefly, about the language default or glance at the property's type – that the property has no object semantics. I'd certainly call that "serving a purpose" when maintaining code. – Tim Nov 17 '14 at 16:58