简体   繁体   English

将NSDictionary值转换为NSInteger

[英]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: 我有一个包含值的NSDictionary,我需要获取此值,我尝试使用以下代码获取该值:

[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); } 

The setup of the timer appears to be wrong. 计时器的设置似乎错误。 You can not pass more than one dictionary to the userInfo parameter. 您不能将多个词典传递给userInfo参数。

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. 编辑:添加了重复参数,感谢Bavarious。

Your userInfo is not built correctly. 您的userInfo构建不正确。 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: . 编辑:如果您想传递具有多个键和值的字典,则可以使用dictionaryWithObjects:forKeys:进行操作。

Moszi Moszi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM