synthesize

How dangerous is it to use pointer-style assignment versus setter-methods in Objective-C?

醉酒当歌 提交于 2019-11-29 16:15:10
Lets say I have a simple class like the following: @interface A { // @public int var; } // @property(some_property) int var; @end When I want to access the variable var, I have some options. If I make var public, I can do this: A objectA = [ [ A alloc ] init ]; NSLog( @"%d", objectA->var ); objectA->var = someNumber; If I make it a property instead, I'll have to do something more like this: A objectA = [ [ A alloc ] init ]; NSLog( @"%d", objectA.var ); // dot-syntax NSLog( @"%d", [ objectA var ] ); // get-syntax [ objectA setVar: someNumber ]; I've tried both and they work fine but my question

Objective-C - readonly properties not synthesized with underscore ivar?

人走茶凉 提交于 2019-11-29 02:53:30
If I understand correctly, in Objective-C, properties are automatically synthesized with getters and setters, with the instance variable declared as the property name with an underscore prepended ( _ivar ). So, this code: main.m #import <Foundation/Foundation.h> #import "hello.h" int main(int argc, char *argv[]) { @autoreleasepool { Hello *hello = [[Hello alloc] init]; NSLog(@"%@", hello.myString); return 0; } } hello.h #import <Foundation/Foundation.h> @interface Hello : NSObject @property (copy, nonatomic) NSString *myString; @end hello.m #import "hello.h" @implementation Hello -(Hello *

Adding a getter makes using an underscore incorrect syntax

流过昼夜 提交于 2019-11-28 14:07:02
I have a class with the following header: #import <Foundation/Foundation.h> @interface CustomClass : NSObject @property (strong, nonatomic) NSString *foo; @end With the following implementation that does not show any errors: #import "CustomClass.h" @implementation CustomClass - (void) setFoo:(NSString *)foo { _foo = foo; } @end Being a complete beginner to Objective-C, I am baffled when I add the following method to the implementation: - (NSString *)foo { return _foo; } because now there is an error in the method use of undeclared identifier 'title' and it recommends that I change _foo to foo

How dangerous is it to use pointer-style assignment versus setter-methods in Objective-C?

…衆ロ難τιáo~ 提交于 2019-11-28 09:52:24
问题 Lets say I have a simple class like the following: @interface A { // @public int var; } // @property(some_property) int var; @end When I want to access the variable var, I have some options. If I make var public, I can do this: A objectA = [ [ A alloc ] init ]; NSLog( @"%d", objectA->var ); objectA->var = someNumber; If I make it a property instead, I'll have to do something more like this: A objectA = [ [ A alloc ] init ]; NSLog( @"%d", objectA.var ); // dot-syntax NSLog( @"%d", [ objectA

Objective-C - readonly properties not synthesized with underscore ivar?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 17:25:25
问题 If I understand correctly, in Objective-C, properties are automatically synthesized with getters and setters, with the instance variable declared as the property name with an underscore prepended ( _ivar ). So, this code: main.m #import <Foundation/Foundation.h> #import "hello.h" int main(int argc, char *argv[]) { @autoreleasepool { Hello *hello = [[Hello alloc] init]; NSLog(@"%@", hello.myString); return 0; } } hello.h #import <Foundation/Foundation.h> @interface Hello : NSObject @property

Why do I need to write @synthesize when I provide getter and setter?

為{幸葍}努か 提交于 2019-11-27 14:10:14
So the auto synthesize of properties is awesome. However, when you provide both a getter and a setter, you get an error. @property (strong, nonatomic) NSArray *testArray; - (NSArray *)testArray { return _testArray; } - (void)setTestArray:(NSArray *)testArray { _testArray = testArray; } Error: Use of undeclared identifier '_testArray' . Adding @synthesize testArray = _testArray; solves the problem. I am just wondering why this is? When you provide both getter and setter, there is often just no need for an instance variable at all, i.e. when you just forward those messages or store data in other

Under what conditions is @synthesize automatic in Objective-c?

岁酱吖の 提交于 2019-11-27 09:00:57
Under what conditions is @synthesize automatic in Objective-c? Perhaps when using LLVM 3.0 and up? From reading around the net it seems like @synthesize is unnecessary starting with Xcode 4. However I'm using Xcode 4 and receiving warnings when I don't @synthesize a property. Some of the responses to Why don't properties get automatically synthesized seem to imply @synthesize can be omitted at some point under some circumstances. Another (old) reference hinting that @synthesize might be automatic at some point in the future. As of clang 3.2 (circa February 2012), "default synthesis" (or "auto

Adding a getter makes using an underscore incorrect syntax

爷,独闯天下 提交于 2019-11-27 08:17:46
问题 I have a class with the following header: #import <Foundation/Foundation.h> @interface CustomClass : NSObject @property (strong, nonatomic) NSString *foo; @end With the following implementation that does not show any errors: #import "CustomClass.h" @implementation CustomClass - (void) setFoo:(NSString *)foo { _foo = foo; } @end Being a complete beginner to Objective-C, I am baffled when I add the following method to the implementation: - (NSString *)foo { return _foo; } because now there is

When should I use @synthesize explicitly?

半腔热情 提交于 2019-11-27 05:48:44
As far as I know, since XCode 4.4 the @synthesize will auto-generate the property accessors. But just now I have read a sample of code about NSUndoManager , and in the code it noticed that the @synthesize is added explicitly. Like: @interface RootViewController () @property (nonatomic, strong) NSDateFormatter *dateFormatter; @property (nonatomic, strong) NSUndoManager *undoManager; @end @implementation RootViewController //Must explicitly synthesize this @synthesize undoManager; I am feeling puzzled now... When should I add @synthesize explicitly to my code? Gabriele Petronella There's a lot

Under what conditions is @synthesize automatic in Objective-c?

痞子三分冷 提交于 2019-11-26 17:47:47
问题 Under what conditions is @synthesize automatic in Objective-c? Perhaps when using LLVM 3.0 and up? From reading around the net it seems like @synthesize is unnecessary starting with Xcode 4. However I'm using Xcode 4 and receiving warnings when I don't @synthesize a property. Some of the responses to Why don't properties get automatically synthesized seem to imply @synthesize can be omitted at some point under some circumstances. Another (old) reference hinting that @synthesize might be