Mouhamad Lamaa
Mouhamad Lamaa

Reputation: 884

convert NSDictionary value to NSInteger

i have an NSDictionary that contain in value and i need to get this value i have tried to get the value using the following code:

[NSTimer scheduledTimerWithTimeInterval:0.01  
                         target:self
                               selector:@selector(timerMovelabel:) 
                               userInfo:
            [NSDictionary dictionaryWithObject:var forKey:@"var1"]
     , [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:23] forKey:@"var2"]

i have tried to get the value using the following methods

  1. int intvar = [[[timer userInfo] objectForKey:@"var2"] intValue];

  2. NSNumber *numbervar = [[timer userInfo] objectForKey:@"var2"]; NSInteger intvar = [num intValue];

  3. Follows:

    [self method:[[[timer userInfo] objectForKey:@"var2"] intValue]];
    
    
    - (void)timerMovelabel:(NSTimer *)timer {
        //here i execute one of the steps 1,2 and 3 but i didn't get any result all values are null
    }
    
    
    - (void) method:(NSInteger)dir 
    { 
         NSLog(@"%d",dir);
    }
    

Upvotes: 1

Views: 2714

Answers (2)

BVC
BVC

Reputation: 138

The setup of the timer appears to be wrong. You can not pass more than one dictionary to the userInfo parameter.

Try:

[NSTimer scheduledTimerWithTimeInterval:0.01
                                 target:self
                               selector:@selector(timerMovelabel:)
                               userInfo:
         [NSDictionary dictionaryWithObjectsAndKeys: var, @"var1", 
                                                     [NSNumber numberWithInt:23], @"var2",
                                                     nil]
                               repeats:NO];

EDIT: Added repeats parameter, thanks Bavarious.

Upvotes: 2

Moszi
Moszi

Reputation: 3236

Your userInfo is not built correctly. You need to pass only one object in there.

[NSTimer scheduledTimerWithTimeInterval:0.01  
                         target:self
                               selector:@selector(timerMovelabel:) 
                               userInfo:
     [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:23] forKey:@"var2"]
      repeats:NO];

EDIT: If you would like to pass a dictionary with multiple keys and values, then you can do it with dictionaryWithObjects:forKeys: .

Moszi

Upvotes: 1

Related Questions