メソッドの定義
- (void)test
{
NSLog(@"ok");
}
これでclassファイルにtestメソッドが定義できる。
[self test];
これでtestメソッドが実行出来る。
つまりViewControllerクラスにtestメソッドを指定して実行するには以下のようにする。
- (void)viewDidLoad
{
[super viewDidLoad];
[self test];
}
- (void)test
{
NSLog(@"ok");
}
引数がひとつの場合は以下のようにメソッドを定義
- (void)test:(NSString *)str1
{
NSLog(@"%@",str1);
}
次のように実行する
[self test:@"hoge"];
引数が2つの場合は以下のようにメソッドを定義
- (void)test:(NSString *)str1 str2:(NSString *)str2
{
NSLog(@"%@/%@",str1,str2);
}
次のように実行する。2個以上の引数の指定はラベルの指定が必要になる
[self test:@"hoge1" str2:@"hoge2"];
返り値がある場合は以下のように定義。- (void)ではなく返す値の型を指定する。
-(NSString *)test
{
return @"hoge";
}
次のように実行する。
NSString *hoge =[self test];
NSLog(@"%@",hoge);