以下是我在NSTimer上的Objective-C类别,用于对NSTimers进行基于块的触发.我看不出它有什么问题,但我得到的是我传入schedule...
方法的块正在被解除分配,尽管我要求copy
它.
我错过了什么?
typedef void(^NSTimerFiredBlock)(NSTimer *timer);
@implementation NSTimer (MyExtension)
+ (void)timerFired:(NSTimer *)timer
{
NSTimerFiredBlock blk = timer.userInfo;
if (blk != nil) {
blk(timer);
}
}
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
repeats:(BOOL)repeats
callback:(NSTimerFiredBlock)blk
{
return [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:@selector(timerFired:)
userInfo:[blk copy]
repeats:repeats];
}
@end
Run Code Online (Sandbox Code Playgroud) 在调试器(gdb和llvm)中,
我经常这样做:
po self
po myIvar
p (CGPoint)whatEver
Run Code Online (Sandbox Code Playgroud)
并且工作正常,除非我在一个街区内.如何在调试器中访问它们?我不喜欢到处写NSLogs ......
我想内部块在调试器中我需要以不同的方式访问ivars但我不知道如何:(
我想要访问调用者的绑定.这有什么不对?
require 'test/unit'
class BlocksTest < Test::Unit::TestCase
class Blocks
def initialize( &block ); @block = block; end
def run; instance_eval { @block.call }; end
def foo; 'foo'; end
end
def test_say_foo_via_string
assert_equal( 'foo', Blocks.new{ 'foo' }.run )
end
# => successful
def test_say_foo_via_caller_method
assert_equal( 'foo', Blocks.new{ foo }.run )
end
# => NameError: undefined local variable or method `foo'
end
Run Code Online (Sandbox Code Playgroud)
为什么我无法访问给定块中的调用者实例?
这有点意外的行为,可能会咬人初学者.首先,这是打算吗?其次,Perl 6使用什么来猜测要创建哪个对象?它是否开始认为它是Block或Hash并稍后更改,还是最终决定?
你可以用大括号和胖箭头构造一个哈希:
my $color-name-to-rgb = {
'red' => 'FF0000',
};
put $color-name-to-rgb.^name; # Hash
Run Code Online (Sandbox Code Playgroud)
使用另一个Pair符号也会创建哈希.
my $color-name-to-rgb = {
:red('FF0000'),
};
Run Code Online (Sandbox Code Playgroud)
但是,如果没有胖箭,我会得到一个Block:
my $color-name-to-rgb = {
'red', 'FF0000',
};
put $color-name-to-rgb.^name; # Block
Run Code Online (Sandbox Code Playgroud)
还有其他方法来定义哈希,但我问的是这个特定的语法,而不是寻找我已经知道的解决方法.
$ perl6 -v
This is Rakudo version 2017.04.3 built on MoarVM version 2017.04-53-g66c6dda
implementing Perl 6.c.
Run Code Online (Sandbox Code Playgroud) 在Objective-C块的实现中调用超级方法吗?
当我在super上调用一个方法时,会抛出一个EXC_BAD_ACCESS错误但是只要我将这些调用改为[super methodToCall]
,[self methodToCall]
并让消息向上移动响应者链就可以了.
-methodToCall
在该类的实例中没有该块存在的实现,但是在超类中有一个(即,自己继承自的类).
我只是好奇地了解为什么在一个块的实现中调用一个方法在一开始是一个问题(技术上),所以我可以在将来避免它.我怀疑它与块中如何捕获变量以及堆栈和堆有关,但我真的没有具体的想法.
注意:在块存储在属性中之后几秒钟就会调用块实现代码,该属性使用copy,所以我不认为这是块的生命周期的问题,所有看起来都没问题.此外,这只是在iPhone设备(3G)上崩溃,但在iPhone模拟器中没有崩溃.
结果EXC_BAD_ACCESS
:
[self retrieveItemsForId:idString completionHandler:^(NSError *error) {
if (!error) {
[super didRetrieveItems];
} else {
[super errorRetrievingItems];
}
}];
Run Code Online (Sandbox Code Playgroud)
完美,实现-didRetrieveItems
和-errorRetrievingItems
超级类.
[self retrieveItemsForId:idString completionHandler:^(NSError *error) {
if (!error) {
[self didRetrieveItems];
} else {
[self errorRetrievingItems];
}
}];
Run Code Online (Sandbox Code Playgroud) 最近对Smalltalk的介绍启发了我对'纯'面向对象风格的应用和好处.我想以前看过的这红宝石的好处,尽管非对象的存在的取向if
,unless
等结构好像它没有通过随身携带的东西一路.
纯粹在这里,我说的是"一切都是对象"(包括通过块或类似的功能)和没有程序风格的流控制,而是在布尔和集合上使用流控制方法.
然而,即使在像Smalltalk这样的语言中,有些东西也很突出,因为它们不是面向对象的.例如,如果不使用特殊语法(:=而不是'is:'或类似方法)进行变量赋值似乎不可能,并且从函数返回值似乎需要^运算符,它似乎不属于对任何物体.
有没有更进一步的语言?
我试图设计一个帮助方法,它将检索UIManagedDocument,然后打开并返回它,以便我可以从我的应用程序中的几个地方访问相同的UIManagedDocument.
由于我对块不太熟悉,因此我遇到了异步性问题.理想情况下,事件的顺序是这样的:
我可以通过某种方式传递原始块吗?
到目前为止,这是我的代码.任何想法都非常感谢,谢谢.
// This is a dictionary where the keys are "Vacations" and the objects are URLs to UIManagedDocuments
static NSMutableDictionary *managedDocumentDictionary = nil;
// This typedef has been defined in .h file:
// typedef void (^completion_block_t)(UIManagedDocument *vacation);
// The idea is that this class method will run the block when its UIManagedObject has opened
@implementation MyVacationsHelper
+ (void)openVacation:(NSString *)vacationName usingBlock:(completion_block_t)completionBlock
{
// Try to retrieve the relevant UIManagedDocument from managedDocumentDictionary
UIManagedDocument *doc …
Run Code Online (Sandbox Code Playgroud) 我有一个标记为由delayed_job异步处理的函数:
class CapJobs
def execute(params, id)
begin
unless Rails.env == "test"
Capistrano::CLI.parse(params).execute!
end
rescue
site = Site.find(id)
site.records.create!(:date => DateTime.now, :action => "Task Failure: #{params[0]}", :type => :failure)
site.save
ensure
yield id
end
end
handle_asynchronously :execute
end
Run Code Online (Sandbox Code Playgroud)
当我运行这个函数时,我传入一个块:
capjobs = CapJobs.new
capjobs.execute(parameters, @site.id) do |id|
asite = Site.find(id)
asite.records.create!(:date => DateTime.now, :action => "Created", :type => :init)
asite.status = "On Demo"
asite.dev = true
asite.save
end
Run Code Online (Sandbox Code Playgroud)
这在没有delayed_job的情况下运行时工作正常,但是当使用它运行时我得到以下错误
2012-08-13T09:24:36-0300: [Worker(delayed_job host:eagle pid:12089)] SitesHelper::CapJobs#execute_without_delay failed with LocalJumpError: no block given (yield) - …
Run Code Online (Sandbox Code Playgroud) 在Apple的文档中说:块文字(即^ {...})是表示块的堆栈本地数据结构的地址.因此,堆栈本地数据结构的范围是封闭的复合语句,因此您应该避免以下示例中显示的模式:
void dontDoThis() {
void (^blockArray[3])(void); // an array of 3 block references
for (int i = 0; i < 3; ++i) {
blockArray[i] = ^{ printf("hello, %d\n", i); };
// WRONG: The block literal scope is the "for" loop.
}
//for example I invoke the block here
blockArray[1]();
}
void dontDoThisEither() {
void (^block)(void);
int i = random():
if (i > 1000) {
block = ^{ printf("got i at: %d\n", i); };
// WRONG: The block literal scope …
Run Code Online (Sandbox Code Playgroud) p = Proc.new{ puts 'ok' }
Run Code Online (Sandbox Code Playgroud)
有可能在proc中看到ruby代码吗?
inspect
返回内存位置:
puts p.inspect
#<Proc:0x007f9e42980b88@(irb):2>
Run Code Online (Sandbox Code Playgroud)
Ruby 1.9.3