jak przekonwertować NSNumber na int w Objective-C
Przed wstawieniem wartości int do słownika ustawiłem wartość int jako [nsnumber numberWithInt: 2], Teraz Kiedy próbuję odzyskać zawartość słownika, chcę ją z powrotem w formacie int.. jak to zrobić??
Oto Mój kod;
NSMutabelDictionary *dict = [[NSMutableDictionary alloc]init];
int intValue = 300;
[dict setObject:[NSNumber numberWithInt:intValue] forKey:@"integer"];
/ Align = "left" / ........
int number = [dict ObjectForKey:@"integer"];
.. to rzuca wyjątek, że casting jest wymagany.. kiedy robię to w ten sposób..
int number = (int)[dict ObjectForKey:@"integer"];
To nie działa...
Jak rozwiązać ten problem?/
Proszę sugerować..
114
Author: Felix Kling, 2010-08-24
4 answers
Spójrz na dokumentację . Użyj metody intValue
:
int number = [[dict objectForKey:@"integer"] intValue];
185
Author: Felix Kling,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 13:46:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 13:46:41
Powinieneś trzymać się typów danych NSInteger
, jeśli to możliwe. Więc stworzyłbyś taką liczbę:
NSInteger myValue = 1;
NSNumber *number = [NSNumber numberWithInteger: myValue];
Dekodowanie działa metodą integerValue
wtedy:
NSInteger value = [number integerValue];
55
Author: Max Seelemann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 11:29:52
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 11:29:52
Użyj metody NSNumber intValue
4
Author: rano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 11:27:22
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 11:27:22
Mniej wyraziste podejście:
int number = [dict[@"integer"] intValue];
0
Author: baskInEminence,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-15 08:27:01
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-15 08:27:01