DRYな備忘録

Don't Repeat Yourself.

TextFieldに入力された数字をintegerとして取得したい【Xcode】【iOS】

【問題】

.hファイルにて

IBOutlet UITextField *hogehoge;

で定義したテキストフィールドに入力された数字を、

.mファイルにて

int num = hogehoge.text;

NSLog( @"%d",num );

として以下のエラーが出る。

warning: incompatible pointer to integer conversion initializing 'int' with an expression of type 'NSString 

【解決】

http://dev44100.blogspot.com/2009/11/objective-c.html

変数型を変換しなければならない。

numの宣言を intValueメソッドを使って以下のように行う。

int num = [hogehoge.text intValue];