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

  • Home
  • SEARCH
  • 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 7947291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:23:24+00:00 2026-06-04T01:23:24+00:00

I have been looking around online, doing research into how to use blocks. I

  • 0

I have been looking around online, doing research into how to use blocks. I have also decided to set up a basic example to try and understand the way in which they work.

Essentially what I want to do is have a ‘block variable’ (no sure if thats the correct term) in which I can store a block of code. I then want to be able to set the code in this block at pointX (methodA or methodB) in my code, then run the block of code at pointY (methodX).

So to be specific, my question is 3-fold

  1. Using the example below is the setup / usage of blocks correct and valid?
  2. In methodX how do I execute the code inside the block (self.completionBlock)?
  3. When creating the block in methodA and methodB will the code be called there and then? If so how can I stop this from happening (all I want to do is set up the code in the block to be called later)?

I may have completely misunderstood how blocks are used, apologies if this is the case, however I’m relatively new to Objective-C and I’m trying to learn.

Here is my code so far:

.h

typedef void (^ CompletionBlock)();

@interface TestClass : NSObject
{   
    CompletionBlock completionBlock;

    NSString *stringOfText;
    NSString *otherStringOfText;
}

@property(nonatomic, copy)CompletionBlock completionBlock;

@property(nonatomic, retain)NSString *stringOfText;
@property(nonatomic, retain)NSString *otherStringOfText;

- (void)methodA:(NSString *)myText;
- (void)methodB:(NSString *)myText and:(NSString *)myOtherText;
- (void)methodX;

@end

.m

- (void)methodA:(NSString *)myText;
{
    if ([self.stringOfText isEqualToString:@""])
    {
        // Set the variable to be used by the completion block
        self.stringOfText = @"I visited methodA"; // normally make use of myText

        // Create the completion block
        __block TestClass *blocksafeSelf = self;
        self.completionBlock = ^()
        {
            [blocksafeSelf methodA:blocksafeSelf.stringOfText];
            blocksafeSelf.stringOfText = nil;
        };
    }
    else
    {
        // Do some other stuff with self.stringOfText
    }
}

- (void)methodB:(NSString *)myText and:(NSString *)myOtherText;
{
    if ([self.stringOfText isEqualToString:@""] || [self.otherStringOfText isEqualToString:@""])
    {
        // Set the variable to be used by the completion block
        self.stringOfText = @"I visited methodB"; // normally make use of myText
        self.otherStringOfText = @"I also visited methodB"; // normally make use of myOtherText

        // Create the completion block
        __block TestClass *blocksafeSelf = self;
        self.completionBlock = ^()
        {
            [blocksafeSelf methodB:blocksafeSelf.stringOfText and:blocksafeSelf.otherStringOfText];
            blocksafeSelf.stringOfText = nil;
            blocksafeSelf.otherStringOfText = nil;
        };
    }
    else
    {
        // Do some other stuff with self.stringOfText and self.otherStringOfText
    }
}

- (void)methodX
{
    // At this point run the block of code in self.completionBlock...how?!
}

In my example either methodA or methodB will be called first. Then some time later (perhaps from a different class) methodX will be called (only ever after methodA or methodB have been called).

It’s worth noting that the methods methodA, methodB and methodX are all in a singleton class.

NOTE: This is just a dummy example to try and understand the workings of blocks, I’m fully aware there are other ways to achieve the same result.

  • 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-06-04T01:23:25+00:00Added an answer on June 4, 2026 at 1:23 am

    Here’s the code, just to be clear:

    - (void)methodX
    {
        if(self.completionBlock)
            self.completionBlock();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking around online to try to find a complete solution but
I have been looking around online for an answer to this question for several
i have been looking around and haven't quite found anything on doing this and
i have been looking around to find some nice working example of implementing Membership
I have been looking into different database libraries for my online card game (PostgreSQL,
I have been looking around for a while and not found anything useful, also
I have been looking around online for a while now for methods of integration
I have been looking around online at how to post a UIImage to the
i have been looking around to try and work out how to pass 2
I have been looking around for solutions, and tried to implement what is often

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.