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 8375505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:11:41+00:00 2026-06-09T15:11:41+00:00

I have a very simple class (currently used as a testing class), which uses

  • 0

I have a very simple class (currently used as a testing class), which uses delegate/protocol methods to interface with it’s parent class. However, I would really like to convert this to use blocks. Yet I can’t find a good resource or tutorial out there to help me figure out how to do this. All the blocks tutorials are just way to complicated, and I would really just like a small, concise example of how to do this.

I currently have the class:

#import <Foundation/Foundation.h>

@protocol TestObjectDelegate <NSObject>

@optional
-(void)testObjectSucceeded:(BOOL)passedTest;
-(void)testObjectedFailed:(NSError *)error;
@end

@interface TestObject : NSObject {
    id<TestObjectDelegate> _delegate;
}
-(void)compare:(NSString *)stringA with:(NSString *)stringB;

@end


#import "TestObject.h"



@implementation TestObject 

- (id)initWithDelegateController:(id<TestObjectDelegate>)delegate {
    self = [super init];
    if (self) {
        _delegate = delegate;
    }
    return self;
}

-(void)compare:(NSString *)stringA with:(NSString *)stringB {
    if ([stringA isEqualToString:stringB]) {
        if(_delegate && [_delegate respondsToSelector:@selector(testObjectSucceeded:)]) {
            [_delegate testObjectSucceeded:YES];
        }
        else {
            [_delegate testObjectSucceeded:NO];
        }
    }
    else {
        if(_delegate && [_delegate respondsToSelector:@selector(testObjectedFailed:)]) {
            [_delegate testObjectedFailed:nil];
        }
    }
}
@end

How could I begin to convert this to a blocks based function? Also, I know ‘retain cycles’ are something to watch out for when implementing a blocks function. What would I need to watch out for when converting this class to use blocks instead of delegate/protocols? Googling ‘retain cycles’ also gives some overly complicated answers.

Any starting pointers would be much appreciated?

  • 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-09T15:11:42+00:00Added an answer on June 9, 2026 at 3:11 pm

    Maybe this example gives you an idea:

    typedef void (^MyCallbackBlock)(BOOL);
    
    @interface TestObject : NSObject {
    
    }
    @property (nonatomic, copy) MyCallbackBlock myBlock;
    @end
    
    
    
    
    #import "TestObject.h"
    
    @implementation TestObject 
    
    -(void) yourMethod
    {
        ...
            self.myBlock(YES); // call block with argument
        ...
    }
    
    - (void)dealloc 
    {
        [myBlock release];   
        myBlock = nil;
        [super dealloc];
    }
    
    @end
    

    When using the object you can then define the block like this:

    TestObject* theTestObject = [[TestObject alloc] init];
    theTestObject.myBlock = ^(BOOL theParameter){
        NSLog(@"foo");
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that uses org.apache.ant and the class is very simple, it's
I have a very simple application which currently has a single Linq to Sql
I currently have a very simple class: public class Music { private Sequence sequence;
I have a very simple WCF4 Restful web service which uses WcfRestContrib to allow
I have a public class called Profile . Very simple model class currently with
I have a very simple class with some char* members. Is strcat the best
So I have a very simple class that has a method called getThumbUrl() but
I have this very simple C++ class: class Tree { public: Node *head; };
Let's say I have a very simple PrototypeJS class that looks like this: var
i'm missing something fundamental here. i have a very simple custom class that draws

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.