Xcode Simulation shows value in UITableView Subtitle, but IPad shows <null>
-
22-12-2019 - |
Question
In my Project, I create a String with various different variables and then insert it into my subtitles. The Simulator shows it spot on. Yet on the IPad it shows NULL in the case of just one variable and not any other. Has anybody ever experienced a similar problem?
As I am unable to post Images (not enough reputation so far) I simply have to reconstruct it by typing it down:
IOS Simulator:
................................................................
Thomas Winter
2014-01-05 - Dauer:0,5 h
................................................................
Real Device:
................................................................
Thomas Winter
2014-01-05 - Dauer:(null) h
................................................................
The String is beeing put together by using a initStringWithFormat method, in which I include the variables. Seeing how the h is still there in the subtitle but noch the Duration before it, I can only think that the variable is defunct on the actual device
NSNumberFormatter * formatter = [[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSString * dauerString =[[NSString alloc]initWithFormat:@"Dauer: %@h",[formatter numberFromString:[[self.leistungListe objectAtIndex:indexPath.row]getDauer]]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd"];
NSString * dateString = [dateFormat stringFromDate:[[self.leistungListe objectAtIndex:indexPath.row]getDatum]];
NSString * detailTextString = [[NSString alloc]initWithFormat:@"%@ - %@",dateString,dauerString];
cell.detailTextLabel.text =detailTextString;
Solution
It's probably the locale being set differently on your simulator and device. If the string you are converting into a number is of a fixed format, you need to specify this exact format, including the decimal and thousand separators used.
Setting a number formatter to "decimal style" takes on various settings from the device or simulator, notably the character used to separate decimals - in much of Europe it is a comma (e.g. Pi is 3,14159....) but elsewhere it is a period (3.14159). The string you are converting will have one of these, which will match the decimal separator used in the locale of the simulator, and the locale of your device will have the other one. When a number formatter reads a string it can't convert, it returns nil, which is what you are seeing.
Manually set the format, or consider a different method - there's no reason to hold numbers as strings beyond initial parsing from a text file, web service or user entry.