instance-variables

Is there automatic initialization for attributes?

半世苍凉 提交于 2019-12-12 23:45:35
问题 I was wondering if attributes were automatically set to nil during an object's initialization or they have random values? 回答1: If this question is indicative of Objective-C, I'd say that you should initialize all values of a variable when you declare them. Explicitly initializing variables when they're declared gives you two benefits: There is no ambiguity in what the value of the variable is. Readability for others who read your code. 回答2: All instance variables are guaranteed to be

use parameterized constructor in other classes constructor

爱⌒轻易说出口 提交于 2019-12-12 14:07:35
问题 I fear that this is a very basic question, however, I was not able to solve it yet. I have a class A // classA.h ... class ClassA { public: ClassA(); ClassA(int foo); private: int _foo; ... } // classA.cpp ClassA::ClassA() { _foo = 0; } ClassA::ClassA(int foo) { _foo = foo; } ... A second class B uses an instance of class A in the constructor: // classB.h ... #include "classA.h" #define bar 5 class ClassB { public: ClassB(); private: ClassA _objectA; ... } // classB.cpp ClassB::ClassB() {

Instance Variables in Rails Model

断了今生、忘了曾经 提交于 2019-12-12 08:12:34
问题 I want to initialize an instance variable within my Rails model that will hold an array and I want to access this variable in other methods within my model. I tried this: class Participant < ActiveRecord::Base @possible_statuses = [ 'exists', 'paired', 'quiz_finished', 'quiz_results_seen', 'money_sent' ] def statuses @possible_statuses end But when I tried the following using rails console: Participant.first.statuses I am returned nil :( Why does this happen? Is there a way to accomplish what

Trying to create a portfolio of stocks with a total value in Objective-C, but unsure of the approach

点点圈 提交于 2019-12-12 05:16:04
问题 I'm currently teaching myself Objective-C with the Big Nerd Ranch guide (2nd edition) and I have not had a problem up until chapter 21. I checked the book's forum, but it was of no help because most people there are newbies with spaghetti code, and I do not want to adopt bad habits...I am a beginner too, as this is my first major language, but spaghetti code is obvious when you see it. I have to take my code from a previous chapter and make a tool that creates an instance of a BNRPortfolio

ruby: class instance variables vs instance variables

不问归期 提交于 2019-12-12 04:37:00
问题 my idea is to create a community wiki for people that come from a java background because reading a lot of explanations, I couldn't comprehend anything until I actually tried a couple of things and the pieces of the puzzle started to find their places. But I first need to make sure I'm getting it right. Coming from such background it was very confusing for me to find out that @variable may mean 2 very different things. Here is an example: class Test @ins = "gah" def self.ins puts @ins end def

Prevent altering instance variable

痴心易碎 提交于 2019-12-12 04:01:22
问题 I would like to allow the user to change self.path after instantiation but not any other instance variable. However, if self.path is changed then the other instance variables should be reevaluated. Is this possible? class File(object): def __init__(self, path): self.path = os.path.abspath(path) self.name = os.path.basename(self.path) self.parent = os.path.dirname(self.path) self.extension = self._get_extension() self.category = self.get_category(self.extension) self.exists = os.path.isfile

Purpose of model variable in respond_with

余生长醉 提交于 2019-12-12 02:39:10
问题 In Rails 3, using the new respond_to and respond_with constructs, I do this: class SurveysController < ApplicationController respond_to :html def survey @program_id = params[:program_id] @participant_id = params[:participant_id] respond_with [@program_id, @participant_id] end end When the view is displayed (survey.html.erb) the variables program_id, @participant_id are both properly set up. However, if I omit them from the respond_with, as follows: class SurveysController <

My iPhone app crashes after returning from background. Cause: UIImage problem

◇◆丶佛笑我妖孽 提交于 2019-12-11 16:01:05
问题 First off, I want to say this site is AWESOME! and it helped me do lots of stuff while creating my iPhone app. Now, my problem is: When I launch my app, I have a UIImageView that loads an image depending on an if/else statements in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method. These images are assigned as follows: BG.image = someImage; of course, BG is the UIImageView, and someImage is an iVar with @property, @synthesis. someImage is initialized with an image from

@pages instance variable is nil:NillClass - Building a simple CMS in Rails

老子叫甜甜 提交于 2019-12-11 15:13:15
问题 After several hours of failed attempts, I am resorting to posting my question here. I have a pages controller as follows: class PagesController < ApplicationController layout "admin" # before_action :confirm_logged_in # before_action :find_subject def index # @pages = Page.where(:subject_id => @subject.id).sorted # @pages = @subject.pages.sorted end def show @page = Page.find(params[:id]) end def new @page = Page.new({:name => "Default"}) @subjects = Subject.order('position ASC') @page_count

Difference between instance attributes and class attributes

做~自己de王妃 提交于 2019-12-11 15:12:28
问题 I'm trying to learn about the instance and class attributes in python. Then am a little confused: is this_obj.var an instance attribute or it belongs to the class attribute. The code is below class Myclass (object): var = 10 this_obj = Myclass() this_obj.somevar = 12 that_obj = Myclass() that_obj.somevar = 12 回答1: Instance and class attributes can be kind of confusing to understand at first. The best way of thinking about it is to pay attention to the name. Instance attributes are owned by