instance-variables

Are synthesized instance variables generated as private instead of protected?

孤街浪徒 提交于 2019-11-29 07:49:35
Since recent runtimes in iOS, we are able to define properties that will generate accessors for instance variables. From what I understand, it is not mandatory to declare the instance variable used since it will be automatically done for us. For example, if I write: @interface MyFirstClass @property (readonly, nonatomic) int size; @end and in the .m @implementation MyFirstClass @synthesize size; @end Then an instance variable named "size" will be added for me and a method called "-(int)size" will be implemented. The problem is that when I create a second class MySecondClass which is a subclass

Objective-C. Property for C array

不羁的心 提交于 2019-11-29 07:46:34
I need something like this: @property (nonatomic, retain) int field[10][10]; but this code doesn't work. How to replace it? I need both setter and getter methods You can do it if you wrap the array in a struct. Structs are supported in @property notation (see CGRect bounds on CALayer, for example). First define your struct: typedef struct { int contents[10][10]; } TenByTenMatrix; Then, in your class interface, you can do: @property (assign) TenByTenMatrix field; Note that in this case, you can only get or set the whole array using the property. So you can't do self.field.contents[0][0] = 1;

Inherit class-level instance variables in Ruby?

随声附和 提交于 2019-11-29 05:33:56
I want a child class to inherit a class-level instance variable from its parent, but I can't seem to figure it out. Basically I'm looking for functionality like this: class Alpha class_instance_inheritable_accessor :foo # @foo = [1, 2, 3] end class Beta < Alpha @foo << 4 def self.bar @foo end end class Delta < Alpha @foo << 5 def self.bar @foo end end class Gamma < Beta @foo << 'a' def self.bar @foo end end And then I want this to output like this: > Alpha.bar # [1, 2, 3] > Beta.bar # [1, 2, 3, 4] > Delta.bar # [1, 2, 3, 5] > Gamma.bar # [1, 2, 3, 4, 'a'] Obviously, this code doesn't work.

In Ruby, how can I get instance variables in a hash instead of an array?

天大地大妈咪最大 提交于 2019-11-29 04:03:42
I have a Ruby class. I want to get an instance variable from an argument to a method in that class. I can do get all of the instance variables as an array: self.instance_variables However, I want to get the instance variable named arg , specifically: class MyClass def get_instance_variable(arg) hash_of_instance_variables[arg] end end object.get_instance_variable('my_instance_var') How do I compute hash_of_instance_variables ? Yossi To create a hash of all instance variables you can use the following code: class Object def instance_variables_hash Hash[instance_variables.map { |name| [name,

In Ruby, why after starting irb, foo.nil? says undefined error, and @foo.nil? gives “true”, and @@wah.nil? gives error again?

半腔热情 提交于 2019-11-29 01:38:36
Same in Ruby 1.8.7 and 1.9.2: $ irb ruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo' for #<Object:0x3794c> from (irb):1 ruby-1.8.7-p302 > @bar.nil? => true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized class variable @@wah in Object from (irb):3 why the instance variable treated differently than a local and class variable? In Ruby, most uninitialized or even non-existing variables evaluate to nil . This is true for local variables, instance variables and global variables: defined? foo #=> nil local_variables #=> [] if false foo = 42 end defined? foo #=>

Objective-C: Compiler error when overriding a superclass getter and trying to access ivar

*爱你&永不变心* 提交于 2019-11-29 01:29:30
I'm working on building an iOS 6 app. I have a class TDBeam which inherits from superclass TDWeapon . The superclass TDWeapon declares a @property in the TDWeapon.h file: @interface TDWeapon : UIView @property (nonatomic) int damage; @end I do not explicitly @synthesize the property, as I'm letting Xcode automatically do so. In the subclass TDBeam I override the getter in the TDBeam.m file: #import "TDBeam.h" @implementation TDBeam - (int)damage { return _damage; } @end Xcode auto-completes the getter method name, as expected. But when I attempt to reference the _damage instance variable

Properties for Class and Its Subclasses Only

孤街醉人 提交于 2019-11-29 01:12:41
Is it possible to define properties that are only available to the class they are defined in, and that class's subclasses? Stated another way, is there a way to define protected properties? Technically, no. Properties are really just methods, and all methods are public. The way we "protect" methods in Objective-C is by not letting other people know about them. Practically, yes. You can define the properties in a class extension, and still @synthesize them in your main implementation block. This is possible by using a class extension (not category) that you include in the implementation files

Ruby class instance variables and inheritance

岁酱吖の 提交于 2019-11-28 22:44:40
问题 I have a Ruby class called LibraryItem . I want to associate with every instance of this class an array of attributes. This array is long and looks something like ['title', 'authors', 'location', ...] Note that these attributes are not really supposed to be methods, just a list of attributes that a LibraryItem has. Next, I want to make a subclass of LibraryItem called LibraryBook that has an array of attributes that includes all the attributes of LibraryItem but will also include many more.

Should internal class methods return values or just modify instance variables?

回眸只為那壹抹淺笑 提交于 2019-11-28 21:16:28
I am creating a query builder class that will help in constructing a query for mongodb from URL params. I have never done much object oriented programming, or designed classes for consumption by people other than myself, besides using basic language constructs and using django's built in Models. So I have this QueryBuilder class class QueryHelper(): """ Help abstract out the problem of querying over vastly different dataschemas. """ def __init__(self, collection_name, field_name, params_dict): self.query_dict = {} self.params_dict = params_dict db = connection.get_db() self.collection = db

When to use instance variables and when to use properties

匆匆过客 提交于 2019-11-28 20:57:32
When using Objective-C properties can you stop creating instance variables altogether or do explicit instance variables (not the ones synthesized by the properties) still serve a purpose where properties would be inappropriate? can you stop creating instance variables altogether No, you can't (in a sense). What you can do is stop declaring them if you have properties. If you synthesize a property and you haven't declared the instvar, it will get declared for you, so you are creating an instance variable, just not explicitly. do they still serve a purpose where properties would be inappropriate