instance-variables

Private ivar in @interface or @implementation

穿精又带淫゛_ 提交于 2019-11-27 21:14:17
Is there any reason to declare a private ivar in @interface instead of @implementation ? I see code like this all over the internet (including documentation provided by Apple ): Foo.h @interface Foo : NSObject { @private id _foo; } @end Foo.m @implementation Foo // do something with _foo @end The header file defines the public interface of a class, whereas a private ivar is... well... private. So why not declare it like this? Foo.h @interface Foo : NSObject @end Foo.m @implementation Foo { @private id _foo; } // do something with _foo @end Declaring instance variables in the @implementation is

When to use instance variables and when to use properties

时光总嘲笑我的痴心妄想 提交于 2019-11-27 20:49:34
问题 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? 回答1: 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

Private members in CoffeeScript?

空扰寡人 提交于 2019-11-27 19:48:00
问题 Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class: class Thing extends EventEmitter constructor: (@_name) -> getName: -> @_name Putting the variable in the class makes it a static member, but how can I make it non-static? Is it even possible without getting "fancy"? 回答1: Is it even possible without getting "fancy"? Sad to

Using Instance Variables in Class Methods - Ruby

北城以北 提交于 2019-11-27 18:28:08
I have a class something like below, and I used instance variables (array) to avoid using lots of method parameters. It works as I expected but is that a good practice? Actually I wouldn't expect that worked, but I guess class methods are not working as static methods in other languages. class DummyClass def self.dummy_method1 @arr = [] # Play with that array end def self.dummy_method2 # use @arr for something else end end The reason instance variables work on classes in Ruby is that Ruby classes are instances themselves (instances of class Class ). Try it for yourself by inspecting DummyClass

How to make instance variables private in Ruby?

别等时光非礼了梦想. 提交于 2019-11-27 18:08:52
Is there any way to make instance variables "private"(C++ or Java definition) in ruby? In other words I want following code to result in an error. class Base def initialize() @x = 10 end end class Derived < Base def x @x = 20 end end d = Derived.new Josh Lee Like most things in Ruby, instance variables aren't truly "private" and can be accessed by anyone with d.instance_variable_get :@x . Unlike in Java/C++, though, instance variables in Ruby are always private. They are never part of the public API like methods are, since they can only be accessed with that verbose getter. So if there's any

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

你。 提交于 2019-11-27 18:06:18
问题 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 ? 回答1: To create a hash of all instance variables you can use the

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

旧时模样 提交于 2019-11-27 16:05:01
问题 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? 回答1: In Ruby, most uninitialized or even non-existing variables evaluate to nil . This is true for local variables, instance variables

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

寵の児 提交于 2019-11-27 15:58:34
问题 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

Properties for Class and Its Subclasses Only

拟墨画扇 提交于 2019-11-27 15:55:54
问题 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? 回答1: 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

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

爷,独闯天下 提交于 2019-11-27 13:52:03
问题 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)