views:

3235

answers:

3

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.

+12  A: 

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.)

Ben Stiglitz
This is correct -- it's a bug in the current simulator. There was a SO topic about this (that I can't find - maybe it was a cocoa-dev thread), and also this: http://iphonedevelopment.blogspot.com/2009/06/runtime-madness.html
Daniel Dickison
@Daniel: this comment is worth reposting as answer
Philippe Leybaert
I believe this was fixed in the 4.0 or 4.1 SDK.
Ben Stiglitz
+1  A: 

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
Eric
I know that, but that's not really an answer to my question. It doesn't explain why a feature that works on the iPhone device is not working in the simulator. Not having to "back up" the property is a feature of the "modern" runtime, supported by the iPhone.
Philippe Leybaert
+1  A: 

I'm getting the same issue / error when simply adding a button reply

'Error: synthesized property 'x' must either be named the same as a compatible ivar...etc

#import "ipdev_iDecideNewViewController.h"

@implementation ipdev_iDecideNewViewController
@synthesize decisionText; <----Error Site, I already declared the var in @implem file

-(IBAction) buttonPressed:(id)sender { decisionText.text = @"Go For It!";

}

I really don't know how to get past this and I'm using the lateast Xcode 3.2.1 (64bit)

any help? [email protected] or reply to post would be nice thank you

raln