我正在解析这样的事情:
11/23/10 23:29:57
Run Code Online (Sandbox Code Playgroud)
它没有与之相关的时区,但我知道它在UTC时区(虽然我不是).我怎样才能让Ruby解析它,就像它在UTC时区一样?
理论上,您可以通过以下方式轻松设置背景颜色:
self.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)
在drawRect中,但这没有任何效果.您可以更改视图的大小,边框,子视图等,但不能更改背景颜色.类似的SO查询建议添加一个subView或更改initWithFrame或initWithCoder初始化消息中的backgroundColor.我的问题是,当其他特征可以改变时,为什么它在drawRect中不起作用?
更改视图的图层backgroundColor也没有做任何事情,也许是出于同样的原因.视图的backgroundColor与其图层的backgroundColor之间有什么关系,因为它们不一样?
这里发生了什么?两种形式"除非"之间的细微差别是什么?
> irb(main):001:0> foo = true unless defined?(foo)
=> nil
irb(main):002:0> unless defined?(fooo) ; fooo = false ; end
=> false
Run Code Online (Sandbox Code Playgroud)
谢谢
在Ruby中,当方法有别名时,别名指向原始方法的主体.因此,即使重新定义原始方法,别名也将继续使用原始定义.
class Foo
def bar
"bar"
end
alias :saloon :bar
end
class Foo
def bar
"BAR"
end
end
puts Foo.new.saloon
Run Code Online (Sandbox Code Playgroud)
将返回'bar'而不是'BAR'.有没有办法让轿车使用新的酒吧定义?
编辑:我应该更清楚.这个例子只是一个问题的例子 - 这不是我需要解决的实际问题.当你有链式别名时,问题就更复杂了,例如,在rails的核心中.例如,perform_action由基准测试模块别名,然后由flash模块.所以现在对perform_action的调用实际上是调用perform_action_with_flash执行它的事情,然后有效地调用perform_action_with_benchmarking然后调用原始的perform_action.如果我想覆盖perform_action_with_benchmarking(即使我同意这是一个坏主意 - 请让我们不要讨论它,因为除了这一点),我不能,因为它已被别名,并且据我所知别名指向的是原始perform_action_with_benchmarking的副本,所以即使重新定义它,也没有效果.
我有一个名为Settings的对象,它继承自NSMutableDictionary.当我尝试使用初始化此对象时
Settings *settings = [[Settings alloc] initWithContentsOfFile: @"someFile"]
Run Code Online (Sandbox Code Playgroud)
它返回一个类型的对象NSCFDictionary.因此,它无法识别我的其他方法.例如,当我调用选择器"save"时,它对象:
[NSCFDictionary save]: unrecognized selector sent to instance 0x524bc0
Run Code Online (Sandbox Code Playgroud)
当然,当我使用花园品种初始化时,它是可以的
Settings *settings = [[Settings alloc] init]
Run Code Online (Sandbox Code Playgroud)
我试图将它再次投射到设置,但这不起作用.这看起来很简单 - 我错过了什么?
谢谢
我可以理解在头文件的@interface中定义函数,但为什么实例变量?实例变量不应该是私有的,只能通过消息访问吗?
可能重复:
覆盖模块中定义的另一个方法
这是一些代码:
class Foo
def bar
puts "Original bar"
end
end
module M
def bar
puts "Called M::bar"
end
end
Foo.send(:include,M)
Foo.new.bar
# => Original bar
Run Code Online (Sandbox Code Playgroud)
当"包含"同名方法时,ruby是否会阻止覆盖先前定义的方法?
为了在mac上构建ruby 1.9,我必须安装gcc 4.6.Rails和一堆其他宝石很好.安装mysql或mysql2给了我各种心痛.
Run Code Online (Sandbox Code Playgroud)rubygems> env ARCHFLAGS="-arch x86_64" gem install mysql2 -- --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension.
/Users/ff/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include --with-mysql-config=/usr/local/mysql/bin/mysql_config
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. please check your installation of mysql and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to …Run Code Online (Sandbox Code Playgroud) 想知道Apple应用商店评论流程的更新速度是否比新应用更快.如果我们希望在具有一系列功能的特定日期进入商店,那么发送应用程序以查看功能的子集是否有意义,然后在完成后发送更新?
NSString *foo = @" x ";
NSRange r = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"foo range = %d,%d",r.location, r.length);
Run Code Online (Sandbox Code Playgroud)
结果"foo range = 0,1"
它会返回长度> 1吗?
假设我有一个已经通过alloc/init组合初始化为字符串的变量.如果我通过处理重新分配它,我会有内存泄漏吗,即.
NSString *s = [[NSString alloc] initWithString:someOtherStringVariable];
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
这里有内存泄漏吗?如果是这样,我是否需要创建另一个变量(例如s2),执行此分配,然后释放原始变量?
NSString *s = [[NSString alloc] initWithString:someOtherStringVariable];
NSString *s2 = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[s release];
Run Code Online (Sandbox Code Playgroud)
现在,如果其他字符串是常量,如@"其他字符串",该怎么办?我需要担心泄漏吗?即.
NSString *s = [[NSString alloc] initWithString:@"Some other string"];
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
谢谢