LoginSignup
1
1

More than 5 years have passed since last update.

Objective-cでアサート(assert)したい

Last updated at Posted at 2016-11-23

デフォルトでメッセージ付きのアサートなので注意、条件式のみの普通のアサートはなさそう。

NSAssertの使いどころ - Over&Out その後

こうして

- (id)initWithFileDirectory:(NSString*)directory title:(NSString*)title edit:(BOOL)editable directRun:(BOOL)isDirecRun runDir:(NSString*)runDir {
    self = [super init];
    .
    .
     _isDirectRun = isDirecRun;
    _runDir = runDir;
    // 第1引数が条件、第2引数にメッセージを指定
    NSAssert(_runDir == nil || _isDirectRun, @"Support runDir with only isDirecRun");
    .
    .
    return self;
}

こう呼ぶと

// directRunがNOなのにrunDirにパラメータを設定している
    self = [super initWithFileDirectory:_appDir
                                  title:@"App"
                                   edit:NO
                              directRun:NO
                                 runDir:[FCFileManager pathForDocumentsDirectory]];
    return self;

実行時にエラーを吐いて止まってくれる。

2016-11-23 13:30:25.017 RubyPico[18632:26713716] *** Assertion failure in -[AppViewController initWithFileDirectory:title:edit:directRun:runDir:], /Users/ongaeshi/Documents/RubyPico/App/SelectViewController.m:58
2016-11-23 13:30:25.056 RubyPico[18632:26713716] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Support runDir with only isDirecRun'

アサートは当面実装する予定のないパラメータ設定などを間違えて使われないようにしたい(でもちゃんとエラー処理を実装するほどでもない)ときに便利。

1
1
1

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
1
1