在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) 在bash/GNU工具中是否有一些单行方式阻塞,直到文件中有匹配的字符串?理想情况下,超时.我想避免多行循环.
更新:似乎我应该强调我希望在字符串匹配时结束进程.
最近对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
我在理解return
块,触发器和lambda中的工作方式时遇到了很多麻烦.
例如,在下面的例子中,为什么batman_ironman_proc
工作,而batman_yield
抛出错误?
def batman_ironman_proc
victor = Proc.new { return "Batman will win!" }
victor.call
"Iron Man will win!"
end
def batman_yield
yield
"Iron man will win!"
end
victor = Proc.new { return "Batman will win!" }
puts batman_ironman_proc
#batman_yield(&victor) === This code throws an error.
Run Code Online (Sandbox Code Playgroud) 我有两个名为"promo_en"和"promo_de"的静态CMS块 - 翻译为现有的两个店铺视图"en"和"de".
我想使用模块的layout.xml文件将它们添加到某些模块的侧边栏中.
问题是,如果我使用以下语法添加它们 - 它们都显示忽略我当前所在的商店视图(我希望有一些自动过滤):
<block type="cms/block" name="Promo_de">
<action method="setBlockId"><block_id>promo_de</block_id></action>
</block>
<block type="cms/block" name="Promo_en">
<action method="setBlockId"><block_id>promo_en</block_id></action>
</block>
Run Code Online (Sandbox Code Playgroud)
如果我将它们重命名为"promo"并使用以下语法 - 它可以正常工作,直到我激活Magento的缓存 - 然后CMS块的输出冻结在任何存储视图首先缓存:
<block type="cms/block" name="Promo">
<action method="setBlockId"><block_id>promo</block_id></action>
</block>
Run Code Online (Sandbox Code Playgroud)
关于此事的想法或解决方法受到高度赞赏.
layout block cache-control magento content-management-system
我正在编写一个辅助DSL,以便更轻松地在视图中创建漂亮的菜单ui。undefined method 'safe_append=' for nil:NilClass
当我在多个erb标签之间断开该块时,视图的erb会产生错误,但如果将其粘贴在一个标签中,它的效果很好。我想了解原因-它应该可以在多个标签中使用,并且更加自然。
这不起作用:
<%= @menu.start do -%>
<%= menu_item some_path_in_routesrb,
title: "Dashboard",
details: "12 New Updates",
icon: "feather:home",
highlight: true
%>
<%= menu_item next_path,
title: "Magical stuff",
details: "unicorn registry",
icon: "fontawesome:rainbow",
highlight: true
%>
<% end -%>
Run Code Online (Sandbox Code Playgroud)
但这有效:
<%= @menu.start do
menu_item "#",
title: "Dashboard",
details: "12 New Updates",
icon: "fe:home",
first: true,
highlight: true
menu_item organizations_path,
title: "Organization",
details: "33k Updates",
icon: "fa:university"
end -%>
Run Code Online (Sandbox Code Playgroud)
前面提到start
的菜单方法如下所示
def start(&block)
if block_given?
self.instance_eval(&block) …
Run Code Online (Sandbox Code Playgroud)