ivar

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

夙愿已清 提交于 2019-12-18 04:01:30
问题 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

receive ivar in other class [closed]

微笑、不失礼 提交于 2019-12-12 03:50:56
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I have two view controllers: BSViewController which contains the source ivars number and array , and BSotherViewController which as the target needs to

add __strong ivar at runtime under ARC

北城余情 提交于 2019-12-11 08:59:38
问题 A normal ivar declared in @interface is __strong default. @interface XLPerson : NSObject { NSString *name; // __strong default } @end Now, I create above class at runtime: Class XLPerson = objc_allocateClassPair([NSObject class], "XLPerson", 0); size_t size = sizeof(NSString*); class_addIvar(XLPerson, "name", size, log2(align), @encode(NSString*))); objc_registerClass(XLPerson); However, the ivar named "name" isn't a __strong ivar. While I using object_setIvar() , the Ivar can't hold the

Can a category access instance variables defined in the class it extends?

拥有回忆 提交于 2019-12-10 14:56:14
问题 I know it's not a great idea to try and place properties in a category. Can I access a class' instance variables from within a category that extends it? Or is it necessary to expose an accessor on the class being extended? For example, let's say I have a class called "Person" and its implementation looks like this: #import "Person.h" @interface Person() { NSMutableArray *_friends; } @end @implementation Person - (instancetype)init { self = [super init]; if (self) { _friends = [NSMutableArray

Does the order of instance variable declaration matter in Objective-C?

↘锁芯ラ 提交于 2019-12-10 08:40:53
问题 I was searching the internet for tips to optimizing Objective-C code and came across this link. In the article I saw the note below, which I am not able to understand. 回答1: This article is out of date. It was at one time true that ivars were stored in an Objective-C instance just as the members of a struct are stored, and thus that memory alignment could (marginally) affect access time. Now, however, ivars are indirectly accessed (at least in Apple's runtime); the instance now holds the

iOS, using underscore vs using iVar directly [closed]

蹲街弑〆低调 提交于 2019-12-10 02:54:21
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . 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

Should I use ivars in Objective-C?

百般思念 提交于 2019-12-07 01:55:17
问题 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 practice? Will this end up biting me in the proverbial butt in the long term? I have been reading mixed reviews on what is "right" and "wrong"... 回答1: I generally don't declare ivars, either. I will often use @synthesize foo = foo_; though to prevent

Does the order of instance variable declaration matter in Objective-C?

拈花ヽ惹草 提交于 2019-12-05 12:38:01
I was searching the internet for tips to optimizing Objective-C code and came across this link. In the article I saw the note below, which I am not able to understand. This article is out of date. It was at one time true that ivars were stored in an Objective-C instance just as the members of a struct are stored, and thus that memory alignment could (marginally) affect access time. Now, however, ivars are indirectly accessed (at least in Apple's runtime) ; the instance now holds the offset of the ivar, and uses that to access the variable. Since all those offsets are of the same type, and you

Syntax for accessing instance variables? (Objective-C)

妖精的绣舞 提交于 2019-12-05 08:13:00
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 that it is synthesized. When we want to access it, we first would want to allocate and initialize it. Having programmed in Objective-C for about a month now, I've seen two different forms of syntax. I've seen people do simply aString = [[NSMutableString alloc] initWithString:@"hi"] , where they allocate the string like that; I've also seen people start it

iOS, using underscore vs using iVar directly [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-05 02:53:15
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . 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*