3

I'm trying to build some sample code from an Objective-C book I just got.

I'm using an older (2006, 32-bit, Snow Leopard) MacBook Pro with Xcode 4.2. I get about 8 errors about "inconsistent number of instance variables specified".

I compiled the same code on a newer MacBook Pro (2010, 64-bit, Lion) and everything compiles just fine.

Here's a picture with the code and errors expanded:

Arjan
  • 22,808
  • 11
  • 61
  • 71
Steee
  • 298
  • 2
  • 5
  • 13

3 Answers3

10

Instance variable declarations inside the @implementation { } block is a relatively recent Objective-C feature. As you've discovered, this doesn't work when compiling for 32-bit. The reason is that you also need to be eligible for the "Modern" Objective-C runtime, which according to https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtVersionsPlatforms.html is available on iOS, and OS X 10.5 and later 64-bit. OS X pre-10.5 and OS X 32-bit use the legacy runtime.

So if you need to build for 32-bit OS X, you have to keep instance variable declarations in the @interface block.

bleater
  • 5,098
  • 50
  • 48
1

I was having this issue. Changed from 32/64 bit target architecture to 63-bit only. Fixed.

ericdmann
  • 357
  • 5
  • 20
1

The instance variables need to be declared in the @interface section, not the @implementation:

  @interface Fraction: NSObject {
    int numerator;
    int denominator;
  }
  ...
Caffeine
  • 862
  • 7
  • 6
  • Why would this compile on a newer mac though? If I move those to the interface section it just tells me: "cannot declare variable inside @interface" – Steee Mar 02 '12 at 15:47
  • "Cannot declare…" sounds like you're not including the braces? – zmccord Mar 02 '12 at 15:56
  • doh! This doesn't explain why it would run fine on a fewer mac though :S – Steee Mar 02 '12 at 16:08
  • 3
    Good point, the difference is that 32bit applications use the legacy runtime. Only 64bit applications and iOS apps use the newer runtime. The major difference between the runtimes is that the modern runtime gives you more flexibility with instance variables and properties. I can't seem to find a document describing this exactly, but I would assume this is the reason. – Caffeine Mar 05 '12 at 10:53
  • @StevenP.: Newer versions of gcc and clang let you declare instance variables in the implementation block. You're probably using an older version of your compiler on the older Mac. – mipadi May 20 '13 at 23:23