Readonly Properties in Objective-C?

Solution 1:

You need to tell the compiler that you also want a setter. A common way is to put it in a class extension in the .m file:

@interface YourClass ()

@property (nonatomic, copy) NSString* eventDomain;

@end

Solution 2:

Eiko and others gave correct answers.

Here's a simpler way: Directly access the private member variable.

Example

In the header .h file:

@property (strong, nonatomic, readonly) NSString* foo;

In the implementation .m file:

// inside one of my init methods
self->_foo = @"someString"; // Notice the underscore prefix of var name.

That’s it, that’s all you need. No muss, no fuss.

Details

As of Xcode 4.4 and LLVM Compiler 4.0 (New Features in Xcode 4.4), you need not mess with the chores discussed in the other answers:

  • The synthesize keyword
  • Declaring a variable
  • Re-declaring the property in the implementation .m file.

After declaring a property foo, you can assume Xcode has added a private member variable named with a prefix of underscore: _foo.

If the property was declared readwrite, Xcode generates a getter method named foo and a setter named setFoo. These methods are implicitly called when you use the dot notation (my Object.myMethod). If the property was declared readonly, no setter is generated. That means the backing variable, named with the underscore, is not itself readonly. The readonly means simply that no setter method was synthesized, and therefore using the dot notation to set a value fails with a compiler error. The dot notation fails because the compiler stops you from calling a method (the setter) that does not exist.

The simplest way around this is to directly access the member variable, named with the underscore. You can do so even without declaring that underscore-named variable! Xcode is inserting that declaration as part of the build/compile process, so your compiled code will indeed have the variable declaration. But you never see that declaration in your original source code file. Not magic, just syntactic sugar.

Using self-> is a way to access a member variable of the object/instance. You may be able to omit that, and just use the var name. But I prefer using the self+arrow because it makes my code self-documenting. When you see the self->_foo you know without ambiguity that _foo is a member variable on this instance.


By the way, discussion of pros and cons of property accessors versus direct ivar access is exactly the kind of thoughtful treatment you'll read in Dr. Matt Neuberg's Programming iOS book. I found it very helpful to read and re-read.