iOSアプリ開発の逆引き辞典

iPhone/iPadで使えるアプリ開発のTipsをまとめてみました

カンマ区切りのRGB値をUIColorへ変換する

カンマ区切りのRGB値255,255,255やRGBA値を255,0,255,255からUIColorインスタンスを生成する方法を紹介します。

componentsSeparatedByString:メソッドを使って、区切り文字として,を使って文字列を分割します。分割された文字の数が3つであればRGBとして、4つであればRGBAとして

最後にUIColorクラスのクラスメソッドであるcolorWithRed:green:blue:alpha:メソッドを使ってUIColorインスタンスを返します。

+ (UIColor*)colorWithColumnString:(NSString*)rgba
{
    NSArray* array = [rgba componentsSeparatedByString:@","];

    CGFloat r = 0, g = 0, b = 0, a = 1.0;
    if ([array count] == 3) {
        r = [[array objectAtIndex:0] floatValue] / 255.0;
        g = [[array objectAtIndex:1] floatValue] / 255.0;
        b = [[array objectAtIndex:2] floatValue] / 255.0;
        a = 1.0;
    } else if ([array count] == 4) {
        r = [[array objectAtIndex:0] floatValue] / 255.0;
        g = [[array objectAtIndex:1] floatValue] / 255.0;
        b = [[array objectAtIndex:2] floatValue] / 255.0;
        a = [[array objectAtIndex:3] floatValue];
    }
    
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

このメソッドは、UIColorクラスのカテゴリにしておくと便利かもしれません。

UIPageViewControllerの画面下部に表示されているPageControlを隠す

UIPageViewControllerを継承して必要なデリゲートを実装し終わった時点で、画面下部のPageControlが表示されてしまうのに気付かれるかと思います。

f:id:ch3cooh393:20140226174439p:plain

このPageControlは、UIPageViewControllerのTransition Styleが UIPageViewControllerTransitionStyleScrollになっており、かつ以下の2つのデリゲートメソッドを実装している時に表示されます。

これらのデリゲートメソッドを削除するか

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return [self.pageTitles count];
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}

もしくはUIPageViewControllerのTransition StyleをScrollからPage Curlに変更してください。Storyboardでは下図のScrollになっている箇所を変更します。

f:id:ch3cooh393:20140226174952p:plain

文字列をNSDate型へ変換する

文字列(NSString)型から日付(NSDate)型へ変換します。

ここでは「2011-06-28 21:23:45」という文字列をNSDateFormatterクラスを使ってNSDate型へ変換します。

// フォーマットされる日付文字列
NSString *dateString = @"2011-06-28 21:23:45";

// フォーマット文字列を設定
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// 文字列をNSDateに変換する
NSDate *formatterDate = [fmt dateFromString:dateString];

NSLog(@"出力:%@", formatterDate);

上記のコードを実行すると、下記のような出力を得ることができます。

出力:2011-06-28 12:23:45 +0000

NSDateFormatterクラスが解釈できない文字列を無視する

文字列からNSDate型へフォーマットする時に不要な文字列が含まれている場合は、アポストロフィー(')で不要な文字を挟む事でNSDateFormatterクラスに無視させる事が可能です。

Store KitのVerifyReceipt(レシート有効性確認)をおこなうとJSONデータが返ってくるのですが、そのうちの購入日(purchase_date)のフォーマットが2011-05-23 18:23:45 Etc/GMTとなっています。

前述した方法でNSDateFormatterクラスを使って変換しましたが、残念ながら返っていた値はnilでした。

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"ja"] autorelease]];

NSDate* date = [dateFormatter dateFromString:@"2011-05-23 02:30:01 Etc/GMT"];
NSLog(@"date: %@", date);  // 出力:(null)

Etc/の部分がNSDateFormatterクラスでは解釈することができず、NSDateへの変換に失敗することがわかりました。フォーマッターに無視して欲しい範囲をアポストロフィーで囲ってしまいます。

アポストロフィー(' ')で括られた範囲が、NSDateFormatterクラスの変換時に無視されます。以下のサンプルコードでいうところの「'Etc/'ZZZ」の部分です。

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss 'Etc/'ZZZ"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"ja"] autorelease]];

NSDate* date = [dateFormatter dateFromString:@"2011-05-23 02:30:01 Etc/GMT"];
NSLog(@"date: %@", date);

上記のコードを実行すると、下記のような出力を得ることができます。

出力:2011-05-23 02:30:01 +0000

正しくフォーマットできていることが確認できます。