2013年2月15日金曜日

Unused variable 'xxx' となる・・・

例えば、以下のような定義をしたときに Unused variable 'xxx' のワーニングが出る場合がある。

static const NSString *xxx = @"xxx";


Xcodeや、プリコンパイラのバージョンに依存するかもしれないけど、上記の定義でワーニングが出る場合は、constの位置が関係している。

以下の様に変更するとワーニングはなくなる。
static NSString * const xxx = @"xxx";

2013年2月4日月曜日

AdMobをiOSアプリに組み込む際の注意

AdMob導入方法を以下のページで確認する際、
https://developers.google.com/mobile-ads-sdk/docs/ios/fundamentals

日本語訳の方で見ると、以下のフレームワークを追加するように書いてある。

  • AudioToolbox
  • MessageUI
  • SystemConfiguration
  • CoreGraphics

が、実際には以下のフレームワークも必要。


  • StoreKit
  • AdSupport

英語版のページを見るとちゃんと書いてある。
訳が古いのかな?


2013年2月2日土曜日

実機転送時にエラーがでる。Code Sign error: Certificate identity 'iPhone Developer: <自分の名前>' appears more than once in the keychain. The codesign tool requires there only be one.

実機転送時にエラーがでる。
Code Sign error: Certificate identity 'iPhone Developer: <自分の名前>' appears more than once in the keychain. The codesign tool requires there only be one.

これで検索すると、「キーチェーン内に同じ証明書の期限切れが複数あるから消してね」っていう記事が見つかるんだけど、僕の場合は見つからない。。。



小一時間悩んで、気づいたのは「有効期限切れの証明書」が表示されないようになってました。。。

設定を変更して、ok!
赤い×マークが出るので消していけば、ビルドが通るはずです。

2013年2月1日金曜日

変数命名時にseから始まる名称はやめよう

変数命名時にseから始まる変数名はなるべく回避しよう。

「self」と入力しようとして「se」と打った段階で、
Xcodeのコードアシスト機能で、self以外の変数が選択されてしまう場合があります。
(私の場合は高確率で。。。)

NSString *searchName; なんて変数を作りたいときがありますが、
NSString *conditionName; とかにするといいかも

文字列から数字に変換したい

NSStringクラスには、格納した文字列をプリミティブな型に変換するメソッドが登録されています。

// double型に変換
- (double) doubleValue;

// float型に変換
- (float) floatValue;

// int型に変換
- (int) intValue;

// NSInteger型に変換
- (NSInteger) integerValue;

// long long型に変換
- (long long) longLongValue;

// BOOL型に変換
- (BOOL) boolValue;  

例えば、以下の様な文字列を変換した場合はどうなるでしょう。
NSString *temp = @"1.23";

この様に変換されます。
NSString *temp = @"1.23";
[temp doubleValue];   // (double)1.23
[temp floatValue];    // (float)1.23
[temp intValue];      // (int)1
[temp integerValue];  // (NSInteger)1
[temp longLongValue]; // (long long)1
[temp boolValue];     // (BOOL)YES


ここで少し数値を上げてみます。
NSString *temp = @"1.78";

この様に変換されます。
NSString *temp = @"1.78";
[temp doubleValue];   // (double)1.78
[temp floatValue];    // (float)1.78
[temp intValue];      // (int)1
[temp integerValue];  // (NSInteger)1
[temp longLongValue]; // (long long)1
[temp boolValue];     // (BOOL)YES

まぁ、自動で四捨五入される様な仕様にはなってないですね^^;
勝手にされても困りますが。。。


次に文字列でやってみます。
NSString *temp = @"aaa";
[temp doubleValue];   // (double)0
[temp floatValue];    // (float)0
[temp intValue];      // (int)0
[temp integerValue];  // (NSInteger)0
[temp longLongValue]; // (long long)0
[temp boolValue];     // (BOOL)NO

ゼロになりますので、判断する必要が出た時はこれで良さそうですね。


数値混入でやってみます。
NSString *temp = @"a11";
[temp doubleValue];   // (double)0
[temp floatValue];    // (float)0
[temp intValue];      // (int)0
[temp integerValue];  // (NSInteger)0
[temp longLongValue]; // (long long)0
[temp boolValue];     // (BOOL)NO


数値混入で、先ほどの順序逆転でやってみます。
NSString *temp = @"11a";
[temp doubleValue];   // (double)11
[temp floatValue];    // (float)11
[temp intValue];      // (int)11
[temp integerValue];  // (NSInteger)11
[temp longLongValue]; // (long long)11
[temp boolValue];     // (BOOL)YES

どうやら、解釈の及ぶ所までは頑張って変換してくれるみたいです。