LoginSignup
68

More than 5 years have passed since last update.

Objective-CをつかってiOSアプリを作る際に気にした方が良いTips

Last updated at Posted at 2014-02-10

より簡単で、よりエラーが少ないコードを書くために、「ここはこうしたほうがよい」と見つけたことをまとめたメモ
Let's write "Modern Objective-C".

NSArray

インスタンスの生成は @[] を使おう

Xcode 4.4からNSArray、NSDictionary、NSNumberに対するリテラルが追加された

Beafore
NSArray *arr = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil];

NSMutableArray *mutableArr = [NSMutableArray arrayWithObjects:@"value4", @"value5", @"value6", nil];

After
NSArray *arr = @[@"value1", @"value2", @"value3"]; // nilは不要

NSMutableArray *mutableArr = [@[@"value4", @"value5", @"value6"] mutableCopy]; // NSArray ObjectをつくってからのmutableCopy

スマートになった

引用
http://dev.classmethod.jp/smartphone/iphone/ios-modern-nsarray/

要素の取得には objectAtIndex: は使わない

添字指定が使えるようになった、だいたいのサンプルコードは未だに objectAtIndex: を使っているけどね

Beafore
NSArray *arr = @[@"value1", @"value2", @"value3"];
id obj = [arr objectAtIndex:0];

After
id obj = arr[0];

他の言語と同じ記述になった

NSDictionary

インスタンスの生成は @{} を使おう

こちらもNSArrayと同様にXcode 4.4からリテラルが追加された

Beafore
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"value1", @"key1",
                        @"value2", @"key2",
                        @"value3", @"key3",
                        nil];

NSMutableDictionary *multiDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                      @"value4", @"key4",
                                      @"value5", @"key5",
                                      @"value6", @"key6",
                                      nil];

After
NSDictionary *dictionary = @{@"key1":@"value1",
                             @"key2":@"value2",
                             @"key3":@"value3"};

NSMutableDictionary *multiDictionary = [@{@"key4":@"value4",
                                          @"key5":@"value5",
                                          @"key6":@"value6"}
                                        mutableCopy];

NSMutableDictionaryでmutableCopyを使っているのは、可読性をあげるためらしい。

要素の取得には objectForKey: は使わない

サンプルコードではよくある記述

Beafore
NSDictionary *dictionary = @{@"key1":@"value1",
                             @"key2":@"value2",
                             @"key3":@"value3"};
id obj = [dictionary objectForKey:@"key1"];
// obj = value1

After
id obj = dictionary[@"key1"];
// obj = value1

他の言語と同じ記述になった

要素の追加・置換には setObject:forKey: は使わない

Beafore
NSDictionary *dictionary = @{@"key1":@"value1",
                             @"key2":@"value2"};
NSMutableDictionary *mutableDic =  [dictionary mutableCopy];
[mutableDic setObject:@"value3" forKey:@"key3"];

After
mutableDic[@"key3"] = @"value3";

NSNumber

インスタンスの生成に numberWith... はいらない

こちらもXcode 4.4からリテラルが追加された

Beafore
NSNumber *number = [NSNumber numberWithInt:4];

After
NSNumber *number = @4;

もちろん numberWithLong: や numberWithFloat: などにも使えます
なにより BOOL にも出来るのです

Beafore
NSNumber *number = [NSNumber numberWithBool:YES];
// number = 1

After
NSNumber *number = @YES;
// number = 1

また、式の評価結果にも使えますよ

NSNumber *number = @(2+2);
// number = 4

Enum (列挙型)

Enumの記述方法

定数リストを作成するには、Modern書式で書く

Beafore
enum {
    FIRST,
    SECOND,
    THIRD
};
typedef NSUInteger numbering;

After
typedef enum numbering : NSUInteger {
    FIRST,
    SECOND,
    THIRD
} numbering;

@property

Xcode 4.4から、コンパイラが @synthesize を補完してくれる様になったため、基本は @synthesize を書かなくてもよくなった。
ここの記事はARCを使っていることを前提にしてます。

引用 というか、ここにすべてがある
http://qiita.com/uasi/items/80660f9aa20afaf671f3

プロパティ属性のデフォルト値

属性のデフォルトは atomic, strong である。

@property id object;
// @property (atomic, strong) id object;
@property NSInteger value;
// @property (atomic, assign)  NSInteger value;

atomic は使わない

パフォーマンスが悪化するだけでほとんどメリットがないらしい。

// ×
@property id value;

// ○
@property (nonatomic) id value;

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
68