9
10

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.

Objective-Cでジェネリクスが使えるようになるみたい - Xcode beta7

Last updated at Posted at 2015-06-14

追記:
すでに他の方がまとめてくださっていました!

Xcode7時代のObjective-C
http://qiita.com/cross-xross/items/54b10b93e9026a02e356


Generics. Allow you to specify type information for collection classes like NSArray, NSSet, and NSDictionary. The type information improves Swift access when you bridge from Objective-C and simplifies the code you have to write.

(英語は弱いので)とりあえずXcode beta7でコード書いてみた。

@property (nonatomic, strong) NSMutableArray<NSString *> *strings; // Generics

...

self.strings = @[].mutableCopy;

// add
[self.strings addObject:@"1"];
[self.strings addObject:@(1)]; // Warning: Incompatible pointer types sending 'NSNumber *' to parameter of type 'NSString * __nonnull'

// get
NSString *string = self.strings[0];
NSNumber *number = self.strings[0]; // Warning: Incompatible pointer types initializing 'NSNumber *' with an expression of type 'NSString *'

// print
NSLog(@"string = %@", string);
NSLog(@"number = %@", number);

確かにGenericsが有効になっていて、間違った型を与えたり要求したりするとコンパイラが警告を出してくれる。(初期化では間違った型を与えても警告でなかったりして、完全ではなさそうだけれど)

SwiftとObjective-Cで共存するときに便利って話なのだろうけれども、Objective-Cだけで利用するのもありだと思った。

9
10
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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?