instance-variables

Class Objects and Instance Variables in Objective-C

纵然是瞬间 提交于 2019-12-07 09:42:46
问题 I'm having a hard time wrapping my head around this concept. I'll take the quote exactly from the book: Class objects also inherit from the classes above them in the hierarchy. But because they don’t have instance variables (only instances do), they inherit only methods. Correct me if I'm wrong, but a class object would be this: NSString *aString = [[NSString alloc]initWithString:@"abc.."]; The class object in this case is *aString -- am I correct so far? The thing that confuses me is the

Python | Why is accessing instance attribute is slower than local?

最后都变了- 提交于 2019-12-07 05:04:31
问题 import timeit class Hello(): def __init__(self): self.x = 5 def get_local_attr(self): x = self.x # 10x10 x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; x;x;x;x;x;x;x;x;x;x; def get_inst_attr(self): # 10x10 self.x;self.x;self.x;self.x;self.x;self.x;self.x;self.x;self.x;self.x; self.x;self.x;self.x;self.x;self.x;self.x;self.x;self.x;self.x;self.x; self.x

Class methods (ruby)

北城以北 提交于 2019-12-06 19:38:27
Newbie here, having a hard time understanding Class methods and why I cannot get an attribute to show up correctly in the instance. class Animal attr_accessor :noise, :color, :legs, :arms def self.create_with_attributes(noise, color) animal = self.new(noise) @noise = noise @color = color return animal end def initialize(noise, legs=4, arms=0) @noise = noise @legs = legs @arms = arms puts "----A new animal has been instantiated.----" end end animal1 = Animal.new("Moo!", 4, 0) puts animal1.noise animal1.color = "black" puts animal1.color puts animal1.legs puts animal1.arms puts animal2 = Animal

Objective C's Instance Variables, why should I declare them?

百般思念 提交于 2019-12-06 16:48:36
I'm having a hard time understanding why I need to declare Instance Variables. Let me explain what I mean.. for example.. @interface LearningViewController : UIViewController { UILabel *myText; // <--- Instance Variables } @property (nonatomic,retain) IBOutlet UILabel *myText; -(IBAction)method:(id)sender; @end this can also be done as @interface LearningViewController : UIViewController { //instance variables go here, but are not declared, I just leave this field blank } @property (nonatomic,retain) IBOutlet UILabel *myText; -(IBAction)method:(id)sender; @end as you can see.. in the latter

Sharing instance variables between classes ruby

拥有回忆 提交于 2019-12-06 12:03:06
问题 I know that this question has been answered before, but I can't seem to make any of the solutions work I'm making a ruby wrapper for an API that I need to call. The Interface class does all of the session handling and actual calling of the api, but I want to build helper classes for the functions that I will be performing most frequently. The problem I am having is that I need a way of maintaining one instance of the Interface class across multiple helper classes. Here's the code I have so

Initialising instance variables as null, “” or 0

吃可爱长大的小学妹 提交于 2019-12-06 11:50:31
When initialising variables with default values: What is the difference between: private static String thing = null; and private static String thing = ""; I'm not understanding which is better and why nor what is the best way to deal with other data types. private static int number = 0; private static double number = 0; private static char thing = 0; Sorry I struggle learning new languages. Except for initializing String to an empty string private static String thing = ""; the other assignments are unnecessary: Java will set all member variables of primitive types to their default values, and

New instance of a UIViewController within another UIViewController: Why can't I set an instance variable?

∥☆過路亽.° 提交于 2019-12-06 11:35:50
So I have a UIViewController subclass called MyTabBarViewController that has a UIScrollView. Inside of MyTabBarViewController I'm creating an instance of another UIViewController subclass called PhotoViewController. (Note: I'm doing this so I can set up the IBOutlets using IB) I'm trying to set the label of each PhotoViewController instance from my TabBarViewController. And I init with nib for each PhotoViewController so I was under the impression that each PhotoViewController instance would be wired up to their respective IBOutlets - allowing me to simply set the label name using pvc.label

Changing state of Ruby objects changes other class members?

柔情痞子 提交于 2019-12-06 08:51:01
I have a class that primarily implements some logic around a multi-dimensional array, which is essentially a grid of numbers. These numbers can swap positions, etc.. However, when they swap, other objects of the same class also appear to be modified. I'm not sure why. I'm using instance variables to store the grid, so I don't understand why changes are apparently affecting other class members. Here's a simplified example; class TestGrid attr_accessor :grid @grid = [] def initialize(newgrid) @grid = newgrid end def to_s out = "" @grid.each{|row| out += row.join("|") + "\n" } out end def swap(x,

Passing an instance variable into a form - rails

元气小坏坏 提交于 2019-12-05 20:47:18
This is likely an error due to my minimal understanding of Rails and how to use variables across models, so if there is more code needed to answer it or if my terminology is incorrect, let me know and I will gladly update the question. I have a feed of posts that I want a user to be able to "like." While the following code allows likes to work on an individual post's page - site.com:3000/posts/*post.id* - with the form data being passed of like[liked_post_id]:*post.id* , when I try to submit a like on a profile - site.com:3000/users/*user.id* - which contains a feed of posts, the form data

Why does this ivar need @protected if @protected is the default?

五迷三道 提交于 2019-12-05 17:19:15
@interface AClass : SomeType { @protected NSMutableArray* amINotAlreadyProtected; //? } Why does this code need @protected if @protected is the default? This code was written by a very experienced programmer, but I would omit the specifier myself. There is no need for the keyword @protected as it is the default behavior. However, some programmers tend to use it anyways incase a less experienced programmer comes along at a later date and doesn't know this. It can also be mentioned that it increase code readability incase there are some variables that are protected and other private or public.