ivar

Should IBOutlets be ivars or properties?

霸气de小男生 提交于 2019-12-04 12:18:42
问题 Though I'm sure they exists, I'm having difficulties finding or pinning down an official best practice for declaring outlets in a ViewController. There are 3 options so far as I can see: ivar only property only property backed with an ivar Xcode currently crashes when I try and auto-generate a property by dragging into my ViewController from IB, but from what I remember, doing so creates a property without an ivar. It is also possible to drag into the ivar section and this will create an ivar

Exposing/Synthesizing iVar properties in Objective c

天大地大妈咪最大 提交于 2019-12-04 03:54:44
问题 I have a class that essentially acts as a light weight wrapper class around another class. It holds that other class as an iVar. I want to be able to expose certain properties (quite a few actually) of the iVar, but to do so I have to write out each property accessor like so: - (void) setProperty:(Class *)value{ _iVar.property = value; } - (Class *) property{ return _iVar.property; } Of course, I have to do this for every single property, which is a pain (there are about 30 of them). I would

“property is backed by an ivar” ? What technically does that mean?

自闭症网瘾萝莉.ら 提交于 2019-12-03 16:31:19
So ... I'm still fairly new to Objective C ... taking some iTunes U corses ... doing some exercises and all ... But when you uses to do @synthesize myProperty = _myIvarPropertyNameToUse; ... iOS 5 would create an ivar that would "back" the property. What exactly is going on here as far as where things sit in memory ... (1) Is the ivar a true variable? ... or is it a pointer to the location of the property in the object? (2) The property is on the heap, (being part of the object), right? Is the ivar on the heap as well? I think I may be losing the big picture ... what's the point of having a

Should IBOutlets be ivars or properties?

余生颓废 提交于 2019-12-03 07:50:48
Though I'm sure they exists, I'm having difficulties finding or pinning down an official best practice for declaring outlets in a ViewController. There are 3 options so far as I can see: ivar only property only property backed with an ivar Xcode currently crashes when I try and auto-generate a property by dragging into my ViewController from IB, but from what I remember, doing so creates a property without an ivar. It is also possible to drag into the ivar section and this will create an ivar without a property. This suggests that property-only and ivar only outlets are both OK with apple. So

Exposing/Synthesizing iVar properties in Objective c

可紊 提交于 2019-12-01 19:27:31
I have a class that essentially acts as a light weight wrapper class around another class. It holds that other class as an iVar. I want to be able to expose certain properties (quite a few actually) of the iVar, but to do so I have to write out each property accessor like so: - (void) setProperty:(Class *)value{ _iVar.property = value; } - (Class *) property{ return _iVar.property; } Of course, I have to do this for every single property, which is a pain (there are about 30 of them). I would love to be able to synthesize this but I haven't been able to figure out how. Is it possible to

Want to perform action when __weak ivar is niled

寵の児 提交于 2019-12-01 16:54:36
I have a @class Foo which contains a __weak id bar ivar. Several actions from methods in different classes can cause the object to disappear and thus get bar niled. I want to perform an action when the ivar is automatically niled by ARC. If possible, I would want to avoid turning bar into a property or using Key-Value Observing. Is this even possible? If not, can KVO be used against non-property ivars? I was led here by a duplicate question, here is what I answered: You can't do that with KVO, but you can still get a notification and emulate this by associating an object with your iVar using

Want to perform action when __weak ivar is niled

走远了吗. 提交于 2019-12-01 15:30:56
问题 I have a @class Foo which contains a __weak id bar ivar. Several actions from methods in different classes can cause the object to disappear and thus get bar niled. I want to perform an action when the ivar is automatically niled by ARC. If possible, I would want to avoid turning bar into a property or using Key-Value Observing. Is this even possible? If not, can KVO be used against non-property ivars? 回答1: I was led here by a duplicate question, here is what I answered: You can't do that

What is the correct way of init iVar variables in presence of ARC

我怕爱的太早我们不能终老 提交于 2019-11-30 16:32:08
Example iVar foo , @property (nonatomic) NSString* foo; // inside .h Option 1 @Synthesize foo; //Inside .m foo = [[NSString alloc] init]; // viewDidLoad method Option 2 @Synthesize foo; //Inside .m self.foo = [[NSString alloc] init]; // viewDidLoad method Option 3 @Synthesize foo = _foo; //Inside .m _foo = [[NSString alloc] init]; // viewDidLoad method Why? At so many places I have seen code which has different ways of doing init an Object in Obj - C but which one is the best practise? justin In this regard, ARC is the same as MRC. you have specified all these take place in viewDidLoad . in

Assignment to ivar in a Block via weak pointer

≯℡__Kan透↙ 提交于 2019-11-30 09:52:06
I have a read-only property isFinished in my interface file: typedef void (^MyFinishedBlock)(BOOL success, NSError *e); @interface TMSyncBase : NSObject { BOOL isFinished_; } @property (nonatomic, readonly) BOOL isFinished; and I want to set it to YES in a block at some point later, without creating a retain cycle to self : - (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock { __weak MyClass *weakSelf = self; MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) { [weakSelf willChangeValueForKey:@"isFinished"]; weakSelf -> isFinished_ = YES; [weakSelf

Objective C: Why do we declare ivars in the .h member area if @property seems to do it automatically?

旧时模样 提交于 2019-11-29 03:27:32
In implementing an interface it seems the common method in tutorials and literature is to declare an ivar and then set the @property then @synthesize . @interface MyClass : NSObject { NSString *myString; } @property (nonatomic, retain) NSString *myString; @end However, omitting the explicit declaration and just putting @property has the same effect. @interface MyClass: NSObject { } @property (nonatomic, retain) NSString *myString; @end So how come most people use @property and an explicit declaration? Is it bad form not to? It used to be necessary. There are two different versions of the