LoginSignup
26
23

More than 3 years have passed since last update.

【ぶっちゃけiOSアプリ開発】手っ取り早くObjective-cを理解する | その4 メソッドの書き方

Last updated at Posted at 2017-12-02

細かいことや例外的なことは後回しでいいので、手っ取り早くObjective-cを理解して使い始めたいという人向けです。他言語を経験していることが前提です。
その4はメソッドの書き方です。Javaやc言語に慣れた方にしてみるとかなり違和感ある書き方だと思いますので、その辺をぶっちゃけて説明します。
Swiftにも通ずる書き方ですので、是非覚えておきたいところです。

「戻り値無し」「引数無し」メソッド

QiitaTestという普通のクラスに、consoleWriteというコンソールに文字列を出力するだけの「戻り値無し」「引数無し」のメソッド。
これは問題なく受け入れられるはず。

ヘッダとメインの書き方。

QiitaTest.h
#import <Foundation/Foundation.h>

@interface QiitaTest : NSObject

- (void)consoleWrite;

@end
QiitaTest.m
#import "QiitaTest.h"

@implementation QiitaTest

- (void)consoleWrite {
    NSLog(@"Good Morning!"); // コンソールにメッセージ出力する
}

@end

ViewControllerからメソッドを呼び出す例。

ViewController.m
#import "ViewController.h"
#import "QiitaTest.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    QiitaTest *test = [[QiitaTest alloc] init]; // インスタンス化して初期化

    [test consoleWrite]; // メソッドを呼び出す
}

@end

メソッドを呼び出す時は[]で囲む。testオブジェクトのconsoleWriteメソッドの呼び出し。

「戻り値無し」「引数無し」メソッド
    [test consoleWrite];

実行結果。

2017-11-29 23:02:10.599 QiitaTest[1186:37521] Good Morning!

「戻り値無し」「引数有り」メソッド

二つの引数があるメソッド。
引数の書き方のポイントである。

ヘッダとメインの書き方。

QiitaTest.h
#import <Foundation/Foundation.h>

@interface QiitaTest : NSObject

- (void)consoleWrightWithHeight:(NSInteger)h weight:(NSInteger)w;

@end
QiitaTest.m
#import "QiitaTest.h"

@implementation QiitaTest

- (void)consoleWrightWithHeight:(NSInteger)h weight:(NSInteger)w {
    NSLog(@"Your Height=%ld Weight=%ld", h, w);
}

@end

このメソッドの書き方がポイントである!

- (void)consoleWrightWithHeight:(NSInteger)h weight:(NSInteger)w;
  • 引数は「:」の後に、「(データ型)変数」で書く。この場合、「(NSInteger)h」が第一引数、「NSInteger)w」が第二引数である。
  • 「weight」というのは何か?これはラベルといい、第二引数を分りやすくするための名前である。(後述するメソッドの呼び出し方を見ると便利さが分かる)
  • では第一引数にラベルは無いのか? →無い。
  • 第一引数のラベルはメソッド名が兼ねる。よって、メソッド名は第一引数の事が分かるように命名するのが良い。(このメソッドの場合、~WithHeightとしているのはそのため)

このラベルが他の言語にないものであり、違和感を覚える主たる要因である。

このメソッドをViewControllerから呼び出してみた例。

ViewController.m
#import "ViewController.h"
#import "QiitaTest.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    QiitaTest *test = [[QiitaTest alloc] init];

    [test consoleWrightWithHeight:174
                           weight:65];
}

@end

このメソッドの呼び出し方もポイント!

「戻り値無し」「引数二つ有り」メソッドの呼び出し
    [test consoleWrightWithHeight:174
                           weight:65];
  • 第一引数に174、第二引数に65を与えている。
  • ラベルを指定して「〜Height:174」「weight:65」と書いているので、呼び出し元のコードを見ただけでどういった引数に値を与えているのかが分かる。これがラベルを使うメリットである。普通のc言語だと method(174, 65); となるので、呼び出し元だけ見ても分かりにくい。

実行結果

2017-12-02 18:28:16.142 QiitaTest[4799:410547] Your Height=174cm Weight=65kg

引数がいっぱいある例

QiitaTest.h
#import <Foundation/Foundation.h>

@interface QiitaTest : NSObject

- (void)consoleWrightWithHeight:(NSInteger)hei
                         weight:(NSInteger)wei
                           bust:(NSInteger)bus
                          waist:(NSInteger)wai
                            hip:(NSInteger)hip;

@end
QiitaTest.m
#import "QiitaTest.h"

@implementation QiitaTest

- (void)consoleWrightWithHeight:(NSInteger)hei
                         weight:(NSInteger)wei
                           bust:(NSInteger)bus
                          waist:(NSInteger)wai
                            hip:(NSInteger)hip {
    NSLog(@"Your Height=%ldcm Weight=%ldkg 3Size=%ld-%ld-%ld",
          hei, wei, bus, wai, hip);
}

@end

ViewControllerから呼び出す。

ViewController.m
#import "ViewController.h"
#import "QiitaTest.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    QiitaTest *test = [[QiitaTest alloc] init];

    [test consoleWrightWithHeight:161
                           weight:54
                             bust:81
                            waist:62
                              hip:87];
}

@end 

実行結果。

2017-12-02 18:37:12.407 QiitaTest[4839:414857] Your Height=161cm Weight=54kg 3Size=81-62-87

「戻り値有り」「引数有り」メソッド

戻り値が有り、二つの引数があるメソッド。
これは解説するまでもないと思うので、リストのみ。

QiitaTest.h
#import <Foundation/Foundation.h>

@interface QiitaTest : NSObject

- (double)consoleWrightWithHeight:(double)h weight:(double)w;

@end
QiitaTest.m
#import "QiitaTest.h"

@implementation QiitaTest

- (double)consoleWrightWithHeight:(double)h weight:(double)w {
    h *= 0.01;
    double bmi = w / (h*h);

    return bmi; // BMIを戻り値として返す
}

@end

ViewControllerから呼び出す。

ViewController.m
#import "ViewController.h"
#import "QiitaTest.h"

@interface ViewController ()
@end

@implementation ViewController {
    double b;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    QiitaTest *test = [[QiitaTest alloc] init];

    b = [test consoleWrightWithHeight:174.3
                               weight:65.5]; // 戻り値有りメソッドを呼び出す
    NSLog(@"BMI=%.2f", b);
}

@end

実行結果。

2017-12-02 18:16:53.593 QiitaTest[4774:405342] BMI=21.56

参考URL


ぶっちゃけiOSアプリ開発は、アプリ開発にあたってとりあえず知っておくべきことをズバリ書きます。例外はありますのでご了承ください。

26
23
0

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
26
23