12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Xcode】第二引数以降はラベルをつける【Objective-C】

Last updated at Posted at 2016-07-29

Objective-Cを触り始めて一ヶ月が経ちました。先日言われて初めて気づいたのですが、Objective-Cでは第二引数以降はラベルをつけるのが一般的だそうです。

// 引数が3つの場合
- (戻り値の型)メソッド名:(引数の型)引数1 ラベル:(引数の型)引数2 ラベル:(引数の型)引数3

//@IT Objective-Cのクラスやプロパティ、特徴的なメソッドの使い方 (3/3)
//http://www.atmarkit.co.jp/ait/articles/1404/09/news029_3.html より引用

実はこの引数のラベル、省略することができるので全く気づかなかったという…
##実装

ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *stringArgument1 = @"aiueo";
    NSString *stringArgument2 = @"kakikukeko";
    int intArgument = 5;
    
    [self setMyArgument:stringArgument1 myArgument2:stringArgument2 myArgument3:intArgument];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) setMyArgument:(NSString *)argument1 myArgument2:(NSString *)argument2 myArgument3:(int)argument3;{
     NSLog(@"myArgument1:%@ myArgment2:%@ myArgment3:%d", argument1, argument2, argument3);
}

@end

実行結果

myArgument1:aiueo myArgment2:kakikukeko myArgment3:5

第一引数はメソッド名(ラベル名?)で関連付けができているので新たに明記しないとのこと

##参考
Objective-Cの勉強(2);メソッドの宣言
引数が複数あるメソッドの記述方法

12
13
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?