LoginSignup
3

More than 5 years have passed since last update.

UILocalNotificationで%が表示されない

Last updated at Posted at 2013-12-08

通知センターに気温と湿度を表示しようと思った。%を出そうと思ったら、どうやっても出ない。えー。

NSString *message = [NSString stringWithFormat:@"Temp %.1f℃, Humidity %d%% in %@",
                                               [weather.temperatureC doubleValue],
                                               [weather.humidity intValue],  // これが表示されない
                                               weather.locationName];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.alertBody = message;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.alertAction = @"OPEN";

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

スクリーンショット 2013-12-08 10.39.38.png

ほら出ない!!!

フォーマット文字列内の湿度以降が欠けている。%を正しく表示するにはUILocalNotificationalertBody%%を渡さなければいけないようだ。

このケースだとフォーマット文字列の結果が%%にならなければいけないので、%%%%を渡すことにする。

NSString *message = [NSString stringWithFormat:@"Temp %.1f℃, Humidity %d%%%% in %@",
                                               [weather.temperatureC doubleValue],
                                               [weather.humidity intValue],
                                               weather.locationName];

スクリーンショット 2013-12-08 10.37.43.png

ほらこれで大丈夫!

UILocalNotificationが%を特殊文字として取り扱っているのかな?と思ったんですがドキュメントに何も書かれてないし、詳しい原因は良くわかりません。

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3