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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:39:54+00:00 2026-05-23T15:39:54+00:00

I want a table of state-names for debug/trace messages as I develop my custom

  • 0

I want a table of state-names for debug/trace messages as I develop my custom gesture. What is wrong with this declaration and its usage?

static NSDictionary *dictStateNames = nil;

@implementation MyCustomGesture


@synthesize state;

+(void)initStateNames {
    if (dictStateNames == nil) {
        dictStateNames = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"StateBegan", [NSNumber numberWithInt:UIGestureRecognizerStateBegan],
                          @"StateCancelled", [NSNumber numberWithInt:UIGestureRecognizerStateCancelled],
                          @"StateChanged", [NSNumber numberWithInt:UIGestureRecognizerStateChanged],
                          @"StateEnded", [NSNumber numberWithInt:UIGestureRecognizerStateEnded],
                          @"StateFailed", [NSNumber numberWithInt:UIGestureRecognizerStateFailed],
                          @"StatePossible", [NSNumber numberWithInt:UIGestureRecognizerStatePossible],
                          @"StateRecognized", [NSNumber numberWithInt:UIGestureRecognizerStateRecognized],
                          nil];
    }
}

-(id) init {
    self = [super init];
    if (self) {
        [MyCustomGesture initStateNames];
        state = UIGestureRecognizerStatePossible;
    }
    return self;
}

-(id) initWithTarget:(id)target action:(SEL)action {
    self = [super initWithTarget:target action:action];
    if (self) {
        [MyCustomGesture initStateNames];
        state = UIGestureRecognizerStatePossible;
    }
    return self;
}

+(NSString*) stateName: (UIGestureRecognizerState) state {
    NSString *retName = [dictStateNames objectForKey:[NSNumber numberWithInt:state]];
    if (retName == nil) {
        return [NSString stringWithFormat:@"Unknown state (%@)", state];
    } else {
        return retName;
    }
}

-(NSString*) currentStateName {
    return [MyCustomGesture stateName:state];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s (%@): %@", __FUNCTION__, [self currentStateName], event);
}
  • 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-23T15:39:55+00:00Added an answer on May 23, 2026 at 3:39 pm

    When you store a reference to an object in a static variable, you need to ensure that it doesn’t get deallocated. So you can either send it a retain message, or create with alloc instead of a convenience creation method. For example:

        dictStateNames = [[NSDictionary dictionaryWithObjectsAndKeys:
                          // List of objects and keys...
                          nil] retain];
    

    or this…

        dictStateNames = [NSDictionary alloc] initWithObjectsAndKeys:
                          // List of objects and keys...
                          nil];
    

    Also, you can coalesce your stateNames getter and the initialization code into a single method, so you’ll typically see Objective-C developers write a method like this:

    + (NSDictionary *)stateNames
    {
        static NSDictionary *stateNames;
    
        if (stateNames == nil) {
            stateNames = [NSDictionary alloc] initWithObjectsAndKeys:
                          // List of objects and keys...
                          nil];
        }
    
        return stateNames;
    }
    

    That way, there’s no need to call it in an instance method (which would be wrong anyway, since a new dictionary would be created each time an instance is initialized, and unless you handled it differently, the previous one would be leaked).

    On another (unrelated) note, consider rewriting your init method as follows:

    - (id)init
    {
        return [self initWithTarget:nil action:NULL];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this table i want to create job_id dynamic Jobs Title text Job Description text
I created an enum where I stored some table names. I want it to
I have a table like this: Name State Amount ------------------------------ Pump 1 Present 339
This is my table , use tr, and td. NAME Address CITY STATE ABC
I have a table that stores multiple items for a state and I want
I have an table, Now i want when after filling this table This is
I have a query that does what i want joining table but i need
I have a repeater control outputting some HTML. I want a table which outputs
In a table I want to combine some of the columns, but not in
I want to get table metadata for all table columns. Like type string(varchar2)/int/float/datetime and

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.