一般Ruby问题:在Ruby中,我经常看到类中的代码,但不是方法的一部分.例如:
class DooDad
attr_accessor :foo
end
Run Code Online (Sandbox Code Playgroud)
要么
class Teacher < ActiveRecord::Base
has_many :students
end
Run Code Online (Sandbox Code Playgroud)
我认为attr_accessor
并且has_many
分别使用:foo
或:students
参数调用的方法是正确的吗?如果是这样,何时执行这些类型的语句.我试过这个:
class DooDad
attr_accessor :foo
puts "I happened!"
@foo = 7
end
Run Code Online (Sandbox Code Playgroud)
它似乎没有运行这些new
方法的一部分:
dd = DooDad.new
dd.foo
Run Code Online (Sandbox Code Playgroud)
输出零,从不吐出任何puts
东西
这一切究竟是如何运作的?
我在网上看到的一切都是关于Info.plist的.当我创建项目时,XCode使我成为[应用程序的名称] -Info.plist文件,并且它似乎与我在线看到的示例Info.plist中具有相同的键.如果我保留它的名称或者我应该将其重命名为Info.plist,这样可以吗?
和
我编写了以下代码来切换子视图:
@synthesize switchableView, viewSelector, currentSubview;
//...
if(switchableView.subviews.count != 0)
[[switchableView.subviews objectAtIndex:0] removeFromSuperview]]
self.currentSubview = (veiwSelector.selectedSegmentIndex == 0) ?
[ViewA new] : [ViewB new];
[switchableView addSubview:currentSubview.view];
//[currentSubview release]; //<---crashes if I uncomment this line
Run Code Online (Sandbox Code Playgroud)
如果我注释掉释放线,它似乎运行正常,但我无法理解为什么.这是我理解发生了什么的方式,也许有人可以告诉我哪里出错了:
所以我们考虑一下currentView:
A由"新"消息分配 - 保留计数= A:1
A由setter保留 - 保留计数= A:2
A的视图得到(据称)保留 - 保留计数= A:2.1
下次通过......
A的子视图被释放count = A:2
B由"新"消息分配 - 保留计数= B:1,A:2
A由设定者自动释放 - B:1,A:1
B由设定者保留 - B:1,A:1
什么都没有摆脱A?
那么我应该更改我的代码,还是我错误地了解内存管理在这种语言中的工作方式......或者两者兼而有之?