3

I've been doing Objective-C programming for a few years now. I was listening to a podcast the other day which mentioned something about how Apple has made it easier over the years, and I thought I heard mention of there being no need to manually add instance variables now. Is this true? Here's how I do it currently:

.h:

@interface Class : UIView

@property (nonatomic, strong) NSString *testString;

@end

.m:

@interface Class () {

NSString *_testString;

}

@end

@implementation Class

@synthesize testString = _testString;

Is this work necessary?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Andrew
  • 15,935
  • 28
  • 121
  • 203

5 Answers5

17

This is all you need now

.h:

@interface Class : UIView

@property (nonatomic, strong) NSString *testString;

@end

.m:

@implementation Class

@end
macserv
  • 3,546
  • 1
  • 26
  • 36
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • 3
    Just to note, that the implicit is: `@synthesize testString = _testString;` which creates `_testString` ivar. In case you want different name or no ivar at all, use explicit. – Tricertops May 01 '13 at 19:55
  • If you write the getter AND setter in `.m` for `testString` you have to use `@synthesize`. However, you do not have to write if you write the getter OR setter OR neither. – Ríomhaire May 01 '13 at 22:07
  • @Ríomhaire: if you write getter and setter in `.m`, you do not have to use `@synthesize` – newacct May 02 '13 at 04:17
  • 1
    @newacct You're thoughts on this http://stackoverflow.com/questions/12918388/why-do-i-need-to-write-synthesize-when-i-provide-getter-and-setter ? – Ríomhaire May 02 '13 at 10:38
  • @Ríomhaire: You still don't *need* to use `@synthesize`, all it is doing in that case is creating the ivar for you, which you can do yourself. – dreamlax May 02 '13 at 15:29
  • Would it be fair to say that in the above answer you do _need_ to use it? – Ríomhaire May 02 '13 at 15:50
  • @Ríomhaire: The point of `@synthesize` is to generate a getter/setter pair. So your comment taken literally would be incorrect -- if you already have a getter/setter pair, then that's sufficient. However, it seems you are talking about the side-effect of `@synthesize` (and auto-`@synthesize`) to add an instance variable when there isn't one. I would suggest that that is misuse because the reason `@synthesize` adds an instance variable is to back the auto-generated getter/setter. If you are making a custom getter/setter, then it is your responsibility to add an instance variable if it needs it. – newacct May 02 '13 at 19:53
  • Contextualisation of the comment was inherently implied. I was attempting to clarify that the statement "This is all you need now", in the answer, is not always the case. The generation of the `iVar` is not a 'side-effect', in the stated example, `@synthesize` only generates the `iVar` here; it is the sole effect. Again, with reference to the question '[is there] no need to manually add instance variables now', providing the solution that utilised the `compiler` seemed most appropriate. – Ríomhaire May 02 '13 at 23:03
2

@property will automatically create an instance variable now, and @synthesize is automatically added unless you specify otherwise. So yes, just a @property is enough.

rog
  • 5,351
  • 5
  • 33
  • 40
Chuck
  • 234,037
  • 30
  • 302
  • 389
1

Nope, it will auto-synthesize in Xcode 4.4+

You can read more about it here.

rog
  • 5,351
  • 5
  • 33
  • 40
1

All of that work is unnecessary.

Just declare the property, it will automatically default to creating an instance variable with the underscore convention. Though, self.property may tickle your fancy as well.

You can do the same for private properties by declaring them in an interface extension in the .m file.

@synthesize-ing is no longer necessary. @dynamic is still necessary if I understand correctly

atreat
  • 4,243
  • 1
  • 30
  • 34
1

Although you don't need to type that boilerplate code for non-@dynamic properties since LLVM 4.0 (Xcode 4.4+), it's a good thing to know that it is a compiler feature, not part of the language (Objective C), nor the runtime system. The runtime system still rely on instance variables and getters/setters generated by the @synthesize directive. It's the compiler who is able to generate the code for you, pretty much like it is able to follow conventions and generate calls to retain and release in ARC code.

So, it is important to notice that, if you are going to share your project with other developers using older versions of Xcode (specifically, older versions of the Clang/LLVM compiler), you must synthesize your variables or the project will not compile in their machines or will fail at runtime.

Daniel Martín
  • 7,815
  • 1
  • 29
  • 34