Questions tagged [ivar]

In object-oriented programming, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy, or instance.

In object-oriented programming, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy, or instance.

140 questions
155
votes
7 answers

Why would you use an ivar?

I usually see this question asked the other way, such as Must every ivar be a property? (and I like bbum's answer to this Q). I use properties almost exclusively in my code. Every so often, however, I work with a contractor who has been developing…
Sam
  • 26,946
  • 12
  • 75
  • 101
57
votes
2 answers

Property vs. ivar in times of ARC

It is my understanding that setting an ivar now retains the object being assigned to it, since setting variables defaults to the strong qualifier. Because ivars are in the scope of the object they are declared in and strong retains objects within…
Toastor
  • 8,980
  • 4
  • 50
  • 82
25
votes
5 answers

Reason to use ivars vs properties in objective c

I have been unable to find any information on this topic and most of what I know about it has come by complete accident (and a few hours of trying to figure out why my code wasn't working). While learning objective-c most tutorials I have found make…
CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50
23
votes
1 answer

Objective-C self->_ivar access with explicit vs implicit self->

General Problem Until now, I always thought self->_ivar is equivalent to _ivar. Today I found out that this is not entirely true. See, for example the following code snippet: @interface TestClass : NSObject { NSString…
Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
20
votes
2 answers

Under ARC, are Blocks automatically copied when assigned to an ivar directly?

Assume the following code under ARC, typedef void (^MyResponseHandler) (NSError *error); @interface MyClass : NSObject { MyResponseHandler _ivarResponseHandler; } - (void)myMethod:(MyResponseHandler)responseHandler { _ivarResponseHandler =…
18
votes
1 answer

Why can't categories have instance variables?

I understand we can use associative references to invoke ivar-like behavior in categories. But what's the specific reason behind not being able to declare new ivars in categories? Is it because we would invade the private space of the class? Or is…
Ravi
  • 7,929
  • 6
  • 38
  • 48
15
votes
4 answers

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

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…
anixon604
  • 323
  • 1
  • 9
15
votes
3 answers

iOS automatic @synthesize without creating an ivar

If I have a @property which I didn't want to have backed via an ivar I simply omitted the @synthesize and had manual getters which returned a calculated value. However, now since Xcode 4.4 if I don't specify @synthesize do compiler will…
znq
  • 44,613
  • 41
  • 116
  • 144
12
votes
1 answer

Need to declare a public instance variable in Objective-C

I'm trying to declare some instance variables for a custom button class in Objective-C (for iOS): @interface PatientIDButton : UIButton { NSUInteger patientID; NSString * patientName; } @end However, these are now private and I need them…
easythrees
  • 1,530
  • 3
  • 16
  • 43
11
votes
7 answers

Syntax for accessing instance variables? (Objective-C)

What is the proper syntax for accessing an instance variable in Objective-C? Assume we have this variable: @interface thisInterface : UIViewController { NSMutableString *aString; } @property (nonatomic, retain) NSMutableString *aString; and…
SpacePyro
  • 3,121
  • 4
  • 25
  • 30
9
votes
3 answers

iOS, using underscore vs using iVar directly

This has been asked a lot, but this question is to get examples of when you would use each of these methods. Please use examples other than the setter and getter infinite loop example. .h - @property(nonatomic, strong)NSMutableArray*…
jgvb
  • 304
  • 5
  • 16
8
votes
2 answers

Assignment to ivar in a Block via weak pointer

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…
7
votes
4 answers

Should I use ivars in Objective-C?

I have an application that I'm writing that uses solely @properties. I have not one ivar declared at all in any of my class files. As I understand it ivars are no longer needed with the introduction of @property. Am I coding according to best…
ElasticThoughts
  • 3,417
  • 8
  • 43
  • 58
7
votes
3 answers

Want to perform action when __weak ivar is niled

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…
Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
7
votes
1 answer

Changing an instance variable in a block

I am quite confused about how to change an instance variable inside of a block. The interface file (.h): @interface TPFavoritesViewController : UIViewController { bool refreshing; } The implementation: __weak TPFavoritesViewController…
Keiran Paster
  • 600
  • 2
  • 7
  • 21
1
2 3
9 10