ivar

Objective-C - iVar Scoped Method Variables?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 00:40:43
I was messing around in Objective-C earlier, and I ran into a quite common situation: I had a class, which was not a singleton, that needed a variable shared between method calls, like static , but each instance needed it's own variable. However, this variable only needed to be used in one particular method, we'll call it -foo . What I'd love to do, is have a macro, let's call it ivar , which lets me do the following: @implementation MyClass -(foo) { ivar int someVal = 10; // default value, ivar scoped variable. } -(bar) { someVal = 5; // error, outside of `foo`'s scope. } @end How the

putting ivars into init

只愿长相守 提交于 2019-11-28 14:44:47
I have two view controllers: BSViewController which contains the source ivars number and array , and BSotherViewController which as the target needs to receive the ivars . One way of producing the desired result was provided in this question. The suggestion was to compute the values of self.number and self.array in an init that overrides the designated init as shown here. - (id)init { if (self = [super init]) { //Set values NSArray* _array = [NSArray arrayWithObjects: @"manny",@"moe",nil]; self.array = _array; self.number = 25; } return self; } But I don't know how this solution enables the

iOS automatic @synthesize without creating an ivar

橙三吉。 提交于 2019-11-28 08:25:17
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 automatically generate it. Does that mean it will also generate an ivar even do I don't need/use it? I could eventually force to not auto-synthesize by using dynamic . However that would be wrong, since @dynamic is supposed to be used for turning off warnings if getter and setter are implemented somewhere else or during runtime . In my working with this, I've

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

二次信任 提交于 2019-11-28 08:14:53
Assume the following code under ARC, typedef void (^MyResponseHandler) (NSError *error); @interface MyClass : NSObject { MyResponseHandler _ivarResponseHandler; } - (void)myMethod:(MyResponseHandler)responseHandler { _ivarResponseHandler = responseHandler; ... } Question: Is the block automatically copied to the heap when assigned to the ivar? My previous question implied that it is copied when assigned through a @property . But, today I used the above code and received an EXC_BAD_ACCESS that was fixed by changing to _ivarResponseHandler = [responseHandler copy] . Edit: My previous answer was

Why can't categories have instance variables?

家住魔仙堡 提交于 2019-11-27 15:49:14
问题 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 there any other reason? If yes, I would appreciate an example that shows the ability to declare ivars in categories breaking whatever it breaks. 回答1: Think of the Objective-C's ivars like a plain old C-structure. When you instantiate an instance of

Objective-C - iVar Scoped Method Variables?

回眸只為那壹抹淺笑 提交于 2019-11-27 15:23:34
问题 I was messing around in Objective-C earlier, and I ran into a quite common situation: I had a class, which was not a singleton, that needed a variable shared between method calls, like static , but each instance needed it's own variable. However, this variable only needed to be used in one particular method, we'll call it -foo . What I'd love to do, is have a macro, let's call it ivar , which lets me do the following: @implementation MyClass -(foo) { ivar int someVal = 10; // default value,

putting ivars into init

纵饮孤独 提交于 2019-11-27 08:52:01
问题 I have two view controllers: BSViewController which contains the source ivars number and array , and BSotherViewController which as the target needs to receive the ivars . One way of producing the desired result was provided in this question. The suggestion was to compute the values of self.number and self.array in an init that overrides the designated init as shown here. - (id)init { if (self = [super init]) { //Set values NSArray* _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];

Property vs. ivar in times of ARC

余生长醉 提交于 2019-11-27 03:00:48
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 the scope of the variable, this means the ivars value would never be released while the object containing the ivar is still alive. Is this correct? If so, am I right in thinking that there is, in terms of memory management, no difference between a retaining (strong) property and a simple ivar anymore? If a variable: Is declared in a class using ARC .

iOS automatic @synthesize without creating an ivar

谁说胖子不能爱 提交于 2019-11-27 02:18:56
问题 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 automatically generate it. Does that mean it will also generate an ivar even do I don't need/use it? I could eventually force to not auto-synthesize by using dynamic . However that would be wrong, since @dynamic is supposed to be used for turning off warnings

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

本小妞迷上赌 提交于 2019-11-27 02:08:23
问题 Assume the following code under ARC, typedef void (^MyResponseHandler) (NSError *error); @interface MyClass : NSObject { MyResponseHandler _ivarResponseHandler; } - (void)myMethod:(MyResponseHandler)responseHandler { _ivarResponseHandler = responseHandler; ... } Question: Is the block automatically copied to the heap when assigned to the ivar? My previous question implied that it is copied when assigned through a @property . But, today I used the above code and received an EXC_BAD_ACCESS that