Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Simulator: build errors when using synthesized instance variables

There are two runtimes for Cocoa/Objective-C: the legacy runtime and the "modern" runtime (that's what Apple calls it).

According to Apple's documentation, "iPhone applications and 64-bit programs on Mac OS X v10.5 and later use the modern version of the runtime".

So far so good.

Now, the "modern" runtime supports a feature called "synthesized instance variables", which means that you don't have to define an instance variable for every declared property. The instance variable will be added automatically. Quote from the iPhone Reference Library: "For the modern runtimes, instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used."

If you use this feature in your iPhone app, it builds and runs fine on the iPhone (physical) device, but when you change the target to "iPhone Simulator", you get build errors:

synthesized property 'x' must either be named the same as a compatible ivar or must explicitly name an ivar

What's going on here? Isn't the iPhone simulator a real iPhone simulator? Does this mean that the simulator uses a different runtime than the physical iPhone?

How can I use this feature on the iPhone simulator?

EDIT:

The code which doesn't compile when targeting the iPhone Simulator is:

@interface MyClass : NSObject {
}

@property NSString *prop1;

@end

According to the documentation, this should work fine on the "modern" runtime, and indeed it does on the iPhone device, but it doesn't compile when changing the target to iPhone Simulator.

like image 488
Philippe Leybaert Avatar asked Jul 20 '09 18:07

Philippe Leybaert


2 Answers

The iPhone Simulator in current SDKs (3.0) use the host’s runtime, which does not support synthesized ivars in 32-bit mode. You’ll have to @synthesize your ivars until the Simulator is fixed. (It’d be good to file a bug with Apple requesting this enhancement.)

like image 123
Ben Stiglitz Avatar answered Nov 08 '22 07:11

Ben Stiglitz


You need to have a variable to 'back up' the synthesized property, unless you plan on implementing the property yourself.

The simplest way to fix your code is to add an instance variable:

@interface MyClass : NSObject {
NSString * prop1;
}

@property NSString *prop1;

@end
like image 36
Eric Avatar answered Nov 08 '22 07:11

Eric