xcodeを4.5にした時に出てくる警告の解消方法

xcode4.5にバージョンアップした際に、「Enumeration values 'NSTextAlignmentJustified' and 'NSTextAlignmentNatural' not handled in switch」という警告文が出た時の解消策。

環境

iOS6 Xcode4.5 cocos2d1.x

解決方法

エラーが出ている箇所はFontLabel.mの中の「- (void)drawTextInRect:(CGRect)rect」メソッドの中と、FontLabelStringDrawing.mの「drawOrSizeTextConstrainedToSize」中の2箇所です。 コードは以下のようになっていると思います。

[- (void)drawTextInRect:(CGRect)rect]の方 [c] switch (self.textAlignment) { case UITextAlignmentLeft: break; case UITextAlignmentCenter: point.x += (origSize.width - size.width) / 2.0f; break; case UITextAlignmentRight: point.x += origSize.width - size.width; break; }

[/c]

[drawOrSizeTextConstrainedToSizeの方] [c] if (performDraw) { switch (alignment) { case UITextAlignmentLeft: drawPoint.x = 0; break; case UITextAlignmentCenter: drawPoint.x = (constrainedSize.width - lineSize.width) / 2.0f; break; case UITextAlignmentRight: drawPoint.x = constrainedSize.width - lineSize.width; break; } [/c]

swich構文の中に、defaultの選択肢を用意してあげれば警告は消えます。

変更したコードは以下のようになります。 [c] switch (self.textAlignment) { case UITextAlignmentLeft: break; case UITextAlignmentCenter: point.x += (origSize.width - size.width) / 2.0f; break; case UITextAlignmentRight: point.x += origSize.width - size.width;

                default: //これを追加する
                break;
        }

[/c]

いかがでしょうか。 私も最近xcode4.5に変更したら、エラーは出ないのですが警告がたくさん出てきました。 OSのバージョンアップがあれば、こういった不具合が必ずでてきますので、大変ですね。

[参考サイト]