NSNumberFormatter crashing iPhone SDK 4.0b2
I've got an app that's been in the app store for a while and functions perfectly on OS 3.1 - 3.13. However, when tested on 4.0b2 I noticed that it crashes in the same place every time, but only on the device, never on the simulator. I'm using a 3GS to test.
On loadView I initialize an NSNumberFormatter object which is declared and retained in the interface so I have access to it everywhere. In my method I call it several times to convert string values into nsnumbers to be stored in a mutable dictionary.
Here's an example:
[myDictionary setObject:[myStyleFormatter numberFromString:@"1"] forKey:@"hours"];
[myDictionary setObject:[myStyleFormatter numberFromString:@"30"] forKey:@"minutes"];
[myDictionary setObject:[myStyleFormatter numberFromString:@"10"] forKey:@"seconds"];
For some reason it crashes as soon as it tries to set hours. The error is "attem开发者_Python百科pt to insert nil value (key: hours)"
Have I been doing something wrong all along? Has the api changed for 4.0b2?
Thanks,
Howie
I had the same problem. I tracked it down to a NSNumberFormatter
statement that did not like spaces (or commas) every 3 digits in the numbers. Which is one of the reasons for having a number formatter
.
NSNumber *number = [currencyFormatter numberFromString:mstring];
It is fairly standard code in many examples on the internet, so I suspect a lot will find the problem.
Anyway, I fixed it by getting rid of the spaces
NSArray *sArray = [mstring componentsSeparatedByString:@" "];
[mstring setString:@" "]; //space at beginning is OK, would prefer nil
for (NSString *sElement in sArray) {
[mstring appendString:sElement];
}
Then the currencyFormatter
line worked.
BUT, in another area of my code, that same currencyFormatter
statement worked with no problem. I tried changing code in the area to cause the problem, but I could not.
So, very curious!!!
精彩评论