Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3667420
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:59:54+00:00 2026-05-19T01:59:54+00:00

I am trying to understand how to use blocks on iOS. I have read

  • 0

I am trying to understand how to use blocks on iOS. I have read Apple’s docs but, as usual, they are vague and incomplete and several essential bits of information are not mentioned. I have also googled around without success. This is what I am trying to do as an exercise to understand that.

I have created a block to read a string and compare the string to the previous read. If the strings are not the same, return YES, if they are the same, return NO.

This is how I did:

I declared this on .h

BOOL (^differentStrings)(void);

I declared this on .m, inside viewDidLoad in a viewController

__block NSString * previousString;
__block NSString * currentString;
differentStrings = ^(void){

    currentString = [self getString];
    NSLog(@"%@", currentString); // not printing anything on console

    if (![currentString isEqualToString:previousString]) {
        previousString = currentString;
        return YES;
    } else {
        return NO;
    }
};

This is how I use: I have a thread that does this:

if (differentStrings)
  NSLog (@"strings are different);

These are the problems I have:

  1. the block always return YES (strings are different)
  2. I am not comfortable declaring this inside videDidLoad. How should I declare this, so I can use it globally as a method? Should I put this like I would with a method?
  3. I am calling a method “getString” inside the block. Is it OK?
  4. I find strange to declare the block variables on .m. As I see, I should declare the block variables on .h and then just use them on .m. I have tried to do that, but received an error.
  5. I have setup a debugging point on the first line of the block but it is not stopping there;
  6. NSlog line inside the block do not prints anything. Isn’t the block being called?

Can you guys help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:59:55+00:00Added an answer on May 19, 2026 at 1:59 am

    You’re misunderstanding how blocks work. (Okay, so that’s kinda obvious.) In the same way that previousString is a variable pointing to an NSString, differentStrings is a variable pointing to a block. Not the result of running the block, but rather, the block itself. That is, after you do this:

    __block NSString * previousString;
    __block NSString * currentString;
    differentStrings = ^(void){
    
        currentString = [self getString];
        NSLog(@"%@", currentString); // not printing anything on console
    
        if (![currentString isEqualToString:previousString]) {
            previousString = currentString;
            return YES;
        } else {
            return NO;
        }
    };
    

    differentStrings is a variable pointing to the block.Thus, when you do this:

    if (differentStrings)
    

    …you’re simply checking whether differentStrings contains something other than 0 or NULL. Since it contains a block, it is not empty, so it evaluates to true.

    Remember: differentStrings is a block variable, not a BOOL variable. It contains a block (a function, if you will), which when called will return a bool. Thus, in order to actually run the block, you need to call it. Like this:

    differentStrings();
    

    or, in your case:

    if (differentStrings()) {
        NSLog (@"strings are different");
    }
    

    Edit: As pointed out in the comments, since differentStrings is an instance variable, you need to copy it, just like you’d call retain on any other object assigned to an instance variable. (For technical reasons I won’t go into now, you should always use copy with blocks instead of retain.) Likewise, you’ll need to call release on it at some point later, perhaps in your dealloc method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use reference parameters. There are several examples
I'm trying to understand amazon php sdk for AWS but I really can't use
I have recently started to use Twitter Bootstrap and trying to understand how it
I'm trying to understand blocks. I get how to use them normally, when passed
I am trying to understand how to best make use of blocks in my
The question might sound vague but I'm trying to understand the general concept of
I am learning Django and I am trying to understand the use of models.py
I'm trying to understand how to use firebug to debug my Javascript. So I
I am trying to understand how to use instance variable in Perl OO -
I'm trying to understand why one would use Spring Batch over a scripting language

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.