2

I am trying to understand the fundamentals of blocks. I wrote this simple test:

NSString *(^print_block) () = ^ (NSString *returned_string){
  return @"this block worked!";  
};

NSLog(@"%@", print_block);

I expected console output to be "this block worked!", but instead I get a big flood of error numbers and etc,, ending with:

terminate called throwing an exception

What up?

Edit: the answer has been suggested to use:

NSLog (@"%@", print_block());

But that doesn't work either. The program terminates at the start of the block definition, with the console saying only (lldb) and Xcode putting a little green arrow at the block definition. The arrow reads:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x5f646e71)

I've tried something else that doesn't work:

NSString *(^print_block) () = ^ (NSString *returned_string){
    NSString *return_me = @"this block worked!";
    return return_me;  
};

NSLog(@"%@", print_block);

But at least this doesn't terminate the program. It runs fine. But the console output is still wrong:

<__NSGlobalBlock__: 0x5a58>

Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
  • 1
    try NSLog(@"%@", print_block()); – Vatev Jul 27 '12 at 16:21
  • Doesn't work. I don't get the huge stream of error messages, though, which seems like progress. But the code does terminate without executing. Alternately, I tried: ` NSString *(^print_block) () = ^ (NSString *returned_string){ ` NSString *return_me = @"this block worked!";` ` return return_me; ` ` };` `` ` NSLog(@"%@", print_block);` Now, this doesn't give me any error at all, but it still doesn't work right. The console output is should be `this block worked!` but instead it's `<__NSGlobalBlock__: 0x5a58>`. – Le Mot Juiced Jul 27 '12 at 16:25
  • sorry about the formatting, I can't get this mini-Markdown thing to work right, even though I think I'm doing what it says to. I put ` around all the code parts, but only the ones at the end came out formatted right. And I can't for the life of me make a new line. The help says to add two spaces in a row to make a new line. Two spaces seems to do nothing at all. Anyway, sorry. – Le Mot Juiced Jul 27 '12 at 16:33
  • Four spaces at the beginning of a line make it a 'code' line. Two spaces and a new line make a new line. – Vatev Jul 27 '12 at 16:36
  • @LeMotJuiced The code in your question doesn't give me a big flood of errors. It just prints `<__NSGlobalBlock__: 0x100002430>`, as expected. I think you didn't cut and paste your actual non-working code into your question. – rob mayoff Jul 27 '12 at 16:44
  • @LeMotJuiced http://stackoverflow.com/editing-help#comment-formatting you can't use newlines in comments. only backquotes. – Matt K Jul 27 '12 at 16:45
  • @robmayoff: sorry, I cut and pasted it exactly. Oddly, though, I made my own adjustments to the code later that did get the same result you said. So go figure. I've updated the question. – Le Mot Juiced Jul 27 '12 at 16:47
  • @mkb: That's the exact page I've been looking for to try and do it right, and it's helpful as mud. Two spaces mean new line? Four spaces indent code? What??? – Le Mot Juiced Jul 27 '12 at 16:49
  • @LeMotJuiced it's only helpful if you stop reading after the "Comment Formatting" box. – Matt K Jul 27 '12 at 17:06

1 Answers1

2

Vatev's comment is right on. When you write:

NSLog(@"%@", print_block);

you're passing the block print_block as the argument for the format string in the log statement. You're trying to print the block. This probably results in [print_block description] being called. I don't know if blocks implement a -description method, but if not then you'll get an unrecognized selector exception.

Also, the way you've declared the block is incorrect. You don't need to include the return value in the parameter list.

The following code works as you expect:

NSString *(^print_block)() = ^{
    return @"this block worked!";  
};

NSLog(@"%@", print_block());
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I'll cut and paste my response to Vatev: Doesn't work. I don't get the huge stream of error messages, though, which seems like progress. But the code does terminate without executing. Alternately, I tried: ` NSString *(^print_block) () = ^ (NSString *returned_string){ ` NSString *return_me = @"this block worked!";` ` return return_me; ` ` };` `` ` NSLog(@"%@", print_block);` Now, this doesn't give me any error at all, but it still doesn't work right. The console output is should be `this block worked!` but instead it's `<__NSGlobalBlock__: 0x5a58>`. – Le Mot Juiced Jul 27 '12 at 16:34
  • And I'll cut and paste my apology about the formatting: sorry about the formatting, I can't get this mini-Markdown thing to work right, even though I think I'm doing what it says to. I put ` around all the code parts, but only the ones at the end came out formatted right. And I can't for the life of me make a new line. The help says to add two spaces in a row to make a new line. Two spaces seems to do nothing at all. Anyway, sorry again. – Le Mot Juiced Jul 27 '12 at 16:35
  • It didn't work the way you wrote it because you've specified that the block takes a parameter called `return_string`. See my edit for code that works right. – Caleb Jul 27 '12 at 16:46
  • Yes! Thanks! I thought I was specifying the return value. Thanks so much! – Le Mot Juiced Jul 27 '12 at 16:53