0

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;
Háo Lǎng
  • 43
  • 7

1 Answers1

0

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • The Reason why I make this switch from string to NSNumber to String is because I get the values from a database and the string has a ridicules amount of fraction digits... in order to make it more presentable I have to convert it back and forth because I don't know a different method so far. – Háo Lǎng Jan 11 '14 at 09:33
  • Ok, but can you add the extra information to your question as I asked? – jrturton Jan 11 '14 at 18:38
  • Can't upload pictures due to missing reputation. Tried to reconstruct it as clean as possible – Háo Lǎng Jan 12 '14 at 23:34
  • I asked nicely to get the values from our database without thousands of decimals and got rid of the number formatter. It still works in the Simulator. Now I just have to wait for it to be signed again in order to check. – Háo Lǎng Jan 14 '14 at 08:49