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

The Archive Base Latest Questions

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

I have no clue if what I am trying to do is possible or

  • 0

I have no clue if what I am trying to do is possible or not. I am not experienced with blocks, just read a few tutorials, and really like the idea.
My class AppointmentView.h (and m) is extending a ViewController. It contains a few properties which are members of another Class Appointment, which in turn has a few NSArrays.
For the purpose of creating a grouped tableview, I check the values of the arrays, and then copy appropriately values that will be displayed (nothing too fancy or complicated).
Normally I would go with if-else statements, and control appropriately, however after looking at blocks, it got me thinking if the following is doable:

[self setOutcomes: [[NSArray alloc] initWithObjects:
                        ^{ return [myAppointment.CANCELED isEqualToString:@"NO"] == YES ? 
                                                            [[NSString alloc] initWithString:@"Not Cancelled"] :
                                                            [[NSString alloc] initWithString:@"Cancelled"]; },
                        ^{ return [myAppointment.CANCELED isEqualToString:@"NO"] == YES ?
                                                            [[NSString alloc] initWithString:@"No Cancellation reason"] :
                                                            [[NSString alloc] initWithString:myAppointment.CANCREASON]; },
                        ^{ return [myAppointment EVENTS].length > 0 ?
                                                            [[NSString alloc] initWithString:myAppointment.EVENTS] :
                                                            [[NSString alloc] initWithString:@"No Events"]; },
                        ^{ return [myAppointment SUMMARY].length > 0 ?
                            [[NSString alloc] initWithString:myAppointment.SUMMARY] :
                            [[NSString alloc] initWithString:@"No Summary"]; },
                        nil]];

This code compiles fine, but it crashes when run. My understanding is that this is wrong, because I am telling the enumeration to execute a piece of code every time. So this is not what I want.
Then I tried the following:

   [self setOutcomes: [[NSArray alloc] initWithObjects:
                        [[NSString alloc] initWithString: (NSString *) ^(void){ 
                                                                        return [myAppointment.CANCELED isEqualToString:@"NO"] == YES ?
                                                                                                        @"Not Cancelled" : @"Cancelled"; }],
                        [[NSString alloc] initWithString: (NSString *) ^(void){ 
                                                                        return [myAppointment.CANCELED isEqualToString:@"NO"] == YES ? 
                                                                                                        @"No Cancellation reason" : myAppointment.CANCREASON; }],
                        [[NSString alloc] initWithString: (NSString *) ^(void){ 
                                                                        return [myAppointment.EVENTS isEqualToString:@""] == YES ? 
                                                                                                        myAppointment.EVENTS : @"No Events"; }],
                        [[NSString alloc] initWithString: (NSString *) ^(void){ 
                                                                        return [myAppointment.SUMMARY isEqualToString:@""] == YES ?
                                                                                                        myAppointment.SUMMARY : @"No Summary"; }],
                        nil]];

Which although it compiles fine, crashes with the following error:

-[NSMallocBlock length]: unrecognized selector sent to instance 0x6bd68f0

I also tried typedefining a generic method which I could use, but didnt help either.
So, I was wondering, is this possible ? I know that for this piece of code, I could simply use an if-else (like I normally would) and be done with it, but other circumstances, such a dynamic code generation option would be extremely useful.

I got the feeling that either NSString does not know how to handle this, and I should therefore extend it with an options lets say

initWithBlock:

Or I am missing something else here. Or it may just be not possible…. (?)

PS: I have just seen NSArray’s enumerateUsingBlock: method, but I am not certain if this would serve my purpose in this case, as each entry in the array is completely different from the other, and a specific block would not fit the criteria.

  • 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-04T09:48:24+00:00Added an answer on June 4, 2026 at 9:48 am

    If you need to make simple choices in the initializer you don’t need blocks.

    [NSArray initWithObjects:
             ([myAppointment.CANCELED isEqualToString:@"NO"] ? @"Not Cancelled" : @"Cancelled"),
             ([myAppointment.CANCELED isEqualToString:@"NO"] ? @"No Cancellation reason" : myAppointment.CANCREASON),
             nil];
    

    Use parens to enclose the ?: operator.


    Updated to answer question of how to define and invoke a block at same spot

    This unit test worked for me:

    - (void)testBlockNow
    {
        BOOL X = ^{ return YES; }();
        STAssertTrue(X, nil);
    }
    

    That’s simple: ^{ … } to define the block, then () to invoke the block.

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

Sidebar

Related Questions

I'm trying to install the stored procedure WhoIsActive , but have no clue where
I am trying to make simple testing but failed because I have no clue
I'm trying to add package level annotations but I don't have a clue on
I'm trying to learn how to initialize lists. I have a simple class below
I'm trying to enlarge my regexp knowledge but I have no clue why the
I'm trying to create a daemon thread under Windows, but I have no clue
I'm trying to do something, on wordpress which i'm not sure is possible. So
I have no clue how i can get an existing object structure based on
I have no clue what an anonymous type is in C# nor how it
I have a clue about Object Oriented Programming: I need to have a parent

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.