我如何创建自己的方法,将块作为参数,以后可以调用?
我试过以下事情.
#import <UIKit/UIKit.h>
typedef void (^viewCreator)(void);
@interface blocks2ViewController : UIViewController
{
}
-(void)createButtonUsingBlocks:(viewCreator *)block;
@end
- (void)viewDidLoad {
[super viewDidLoad];
[self createButtonUsingBlocks:^(NSString * name) {
UIButton *dummyButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 200, 100)];
dummyButton.backgroundColor = [UIColor greenColor];
[self.view addSubview:dummyButton];
}];
}
-(void)createButtonUsingBlocks:(viewCreator *)block
{
// Do something
NSLog(@"inside creator");
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将块变量传递给我的自定义方法,但没有任何成功.为什么会这样,做正确的方法是什么?
更新
这是文件is.h
:
#import <UIKit/UIKit.h>
typedef void (^viewCreator)(void);
@interface blocks2ViewController : UIViewController
{
}
- (void)createButtonUsingBlocks:(viewCreator)block;
@end
Run Code Online (Sandbox Code Playgroud)
这是.m
文件:
#import "blocks2ViewController.h"
@implementation blocks2ViewController
// Implement viewDidLoad …
Run Code Online (Sandbox Code Playgroud) 我有理解初始化发生顺序的问题.这是我假设的顺序:
*Once per
1. Static variable declaration
2. Static block
*Once per object
3. variable declaration
4. initialization block
5. constructor
Run Code Online (Sandbox Code Playgroud)
但根据这段代码,我显然是错的:
class SomethingWrongWithMe
{
{
b=0; //no. no error here.
int a = b; //Error: Cannot reference a field before it is defined.
}
int b = 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,错误就会消失:
class SomethingWrongWithMe
{
int b = 0;
{
b=0;
int a = b; //The error is gone.
}
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么没有错误
b=0;
Run Code Online (Sandbox Code Playgroud) 我试图了解NSOperationQueue,并试图创建最简单的例子.我有以下内容:
NSOperationQueue *myOQ=[[NSOperationQueue alloc] init];
[myOQ addOperationWithBlock:^(void){
NSLog(@"here is something for jt 2");
}];
[myOQ addOperationWithBlock:^(void){
NSLog(@"oh is this going to work 2");
}];
Run Code Online (Sandbox Code Playgroud)
但是想这样做:
void (^jt)() = ^void(){
NSLog(@"here is something for jt");
};
void (^cl)() = ^void(){
NSLog(@"oh is this going to work");
};
NSOperationQueue *myOQ=[[NSOperationQueue alloc] init];
[myOQ addOperation:jt];
[myOQ addOperation:cl];
Run Code Online (Sandbox Code Playgroud)
后一种形式可能吗?我可以将块转换为NSOperation吗?
thx提前
你好(对不起我的英文)
该文件 /app/design/frontend/my-theme/default/template/checkout/cart/totals.phtml
具有以下命令:
<tbody>
<?php echo $this->renderTotals(); ?>
</tbody>
Run Code Online (Sandbox Code Playgroud)
在我的主题中,不要在购物车中收取购买价格:... mystore.com/checkout/cart /
$this->renderTotals()
呈现哪个块?
我的问题如下,
我有一个2D numpy数组填充0和1,具有吸收边界条件(所有外部元素都是0),例如:
[[0 0 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0]
[0 0 1 0 1 0 0 0 1 0]
[0 0 0 0 0 0 1 0 1 0]
[0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 1 0 1 0 0 0]
[0 0 0 0 0 1 1 0 0 0]
[0 0 0 1 0 1 0 …
Run Code Online (Sandbox Code Playgroud) 我试图以块循环方式分配我的矩阵.我从这个问题中学到了很多东西(MPI IO读写块循环矩阵)),但这不是我真正需要的.
我来解释一下我的问题.
假设我有这个12 x 12维的矩阵,我希望在2 x 3维度的处理器网格上分布,这样第一个处理器就会得到粗体元素:
A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 …
任何人都可以解释为什么以下代码中止错误
irb(main):186:0> print ((1..10).collect do |x| x**2 end)
SyntaxError: (irb):186: syntax error, unexpected keyword_do_block,
expecting ')'
print ((1..10).collect do |x| x**2 end)
^
(irb):186: syntax error, unexpected keyword_end, expecting $end
print ((1..10).collect do |x| x**2 end)
^
from /usr/bin/irb:12:in `<main>'
Run Code Online (Sandbox Code Playgroud)
而具有相同功能的以下代码是否按预期工作?
irb(main):187:0> print ((1..10).collect { |x| x**2 })
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]=> nil
Run Code Online (Sandbox Code Playgroud)
我确实认为花括号"{}"可以在块定义中任意替换"do end".
我知道我可以通过省略print方法和第一个括号之间的空格来"修复"第一个调用,但我对解释它为什么表现不同感兴趣 .
您好,我正在尝试使用Jade的块并扩展为node.js项目,而ideia是这样的:
layout.jade:
head
script
$(document).ready(function() {
block js_doc_ready
//here goes the doc ready
});
Run Code Online (Sandbox Code Playgroud)
index.jade:
block js_doc_ready
alert('hello!');
alert('one more js line code');
alert('end my js doc ready for this view');
Run Code Online (Sandbox Code Playgroud)
这会给我一个像这样的index.html:
...
<head>
<script type="text/javascript">
$(document).ready(function() {
alert('hello!');
alert('one more js line code');
alert('end my js doc ready for this view');
});
</script>
</head>
...
Run Code Online (Sandbox Code Playgroud)
但是当我看到结果时,'block js_doc_ready'不被视为Jade块.此外,即使它被认为是块,"警报('你好!);' 不会被认为是a,而是Jade标签.
这是我以前在django模板中所做的事情,但是在使用所有这些标签的玉器中,没有做纯HTML的自由我仍然觉得制作这个东西有点太奇怪了.
我正在为Linux内核开发一个新的IO调度程序.我试图看看是否有人知道在Linux中打印出未完成的IO请求总数(磁盘IO队列)的工具?
我会在一个终端工作.
谢谢!
考虑以下C++方法:
class Worker{
....
private Node *node
};
void Worker::Work()
{
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock: ^{
Tool hammer(node);
hammer.Use();
}];
....
}
Run Code Online (Sandbox Code Playgroud)
在捕获"节点"时,块确切地捕获了什么?对于其他情况,块的语言规范http://clang.llvm.org/docs/BlockLanguageSpec.html是明确的:
在复合语句范围内使用的变量以正常方式绑定到块,但自动(堆栈)存储中的变量除外.因此,可以按照人们的预期访问函数和全局变量,以及静态局部变量.[考验我]
块的复合语句中引用的本地自动(堆栈)变量将作为const副本导入并捕获.
但在这里,我们是否捕获了当前的这个值?副本这个使用工人的拷贝构造函数?或者对节点存储位置的引用?
特别是,假设我们说
{
Worker fred(someNode);
fred.Work();
}
Run Code Online (Sandbox Code Playgroud)
当块运行时,对象fred可能不再存在.节点的价值是多少?(假设底层的Node对象永远存在,但是工作者来去.)
相反,我们写了
void Worker::Work()
{
Node *myNode=node;
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock: ^{
Tool hammer(myNode);
hammer.Use();
}];
....
}
Run Code Online (Sandbox Code Playgroud)
结果有何不同?