instance-variables

Should I declare variables in interface or using property in objective-c arc?

北慕城南 提交于 2019-11-26 09:07:11
问题 approach 1: @interface MyController : UIViewController { UILabel *myText; } @property (nonatomic, strong) UILabel *myText; approach 2: @interface MyController : UIViewController @property (nonatomic, strong) UILabel *myText; approach 3: @interface MyController : UIViewController { UILabel *myText; } I have read some articles talking about this kind of stuff but I still do not really realize which approach I have to adopt. I also found that someone said approach 1 is a old way so I would like

Java -What is an instance variable? [closed]

孤人 提交于 2019-11-26 09:00:03
问题 My assignment is to make a program with an instance variable, a String, that should be inputed by user. But I don\'t even know what an instance variable is. What is an instance variable? How do I create one? What does it do? 回答1: Instance variable is the variable declared inside a class, but outside a method: something like: class IronMan{ /** These are all instance variables **/ public String realName; public String[] superPowers; public int age; /** Getters / setters here **/ } Now this

How to pass a default argument value of an instance member to a method?

旧巷老猫 提交于 2019-11-26 08:26:18
I want to pass a default argument to an instance method using the value of an attribute of the instance: class C: def __init__(self, format): self.format = format def process(self, formatting=self.format): print(formatting) When trying that, I get the following error message: NameError: name 'self' is not defined I want the method to behave like this: C("abc").process() # prints "abc" C("abc").process("xyz") # prints "xyz" What is the problem here, why does this not work? And how could I make this work? Adam Wagner You can't really define this as the default value, since the default value is

How to make a real private instance variable?

妖精的绣舞 提交于 2019-11-26 08:03:23
问题 I want to make an instance variable that can\'t be accessed from outside. Is something like that possible in objective-c? I remember Apple has private variables and stuff like that, but if people know about them, they can use them. Apple calls that \"private API\", but obviously others can access that stuff if they find out what\'s in there. Until now I believed that something like this creates a private instance variable: @interface MyClass : NSObject { CGFloat weight; } No @property, no

Difference between self.var and simply var

给你一囗甜甜゛ 提交于 2019-11-26 06:48:39
问题 What is the difference between using self.var vs. just var in an Objective-C class? Are there benefits or dangers to one or the other? 回答1: foo = self.var; self.var = foo; is conceptually identical to foo = [self var]; [self setVar: foo]; So using dot notation, you are really sending messages to self. foo = var; var = foo; is conceptually the same as foo = self->var; self->var = foo; So not using dot notation to access an instance variable is the same as treating self as a pointer to a C

What does @@variable mean in Ruby?

感情迁移 提交于 2019-11-26 06:12:46
问题 What are Ruby variables preceded with double at signs ( @@ )? My understanding of a variable preceded with an at sign is that it is an instance variable, like this in PHP: PHP version class Person { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } Ruby equivalent class Person def set_name(name) @name = name end def get_name() @name end end What does the double at sign @@ mean, and how does it differ from a single at sign

Is there any reason to declare ivars if you're using properties exclusively in Objective-C?

此生再无相见时 提交于 2019-11-26 06:07:51
问题 I tend to use properties exclusively in my classes, especially now that you can declare properties in a class extension thanks to the modern Objective-C 2.0 runtime—I use this feature to create \"private\" properties. My question is if there is any good reason to ever declare ivars in a class interface anymore. I prefer my public-facing interfaces to be as minimal and clean as possible, only revealing aspects of my class that are pertinent. For example, I would tend to do the following:

Static vs Instance Variables: Difference?

谁说我不能喝 提交于 2019-11-26 04:23:36
问题 What is the difference between a static and instance variable. The following sentence is what I cant get: In certain cases, only one copy of a particular variable should be shared by all objects of a class- here a static variable is used. A static variable represents class wide info.All objects of a class share the same data. I thought that instance vars were used class wide whereas static variables only had scope within their own methods? 回答1: In the context of class attributes, static has a

Are ints always initialized to 0?

孤街醉人 提交于 2019-11-26 03:12:30
问题 Is it safe to count on int s always being initialized to 0 in Objective-C? More specifically, when an object with int ivars has been newly instantiated, is it safe to assume that its ivars have value 0? 回答1: Yes, class instance variables are always initialized to 0 (or nil , NULL , or false , depending on the exact data type). See the Objective-C 2.0 Programming Language: The alloc method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all,

Must every ivar be a property?

99封情书 提交于 2019-11-26 02:18:38
问题 I see it recommended all over the place when coding for iOS that properties should be used for accessing instance variables because of the benefits this lends to memory management, among other things. This advice doesn\'t sit terribly well with me. I find that using properties instead of plain old ivars just takes too much code and I don\'t really see the benefits if you\'re comfortable with memory management. Is it really that important? What\'s your approach to managing instance variables?