9
8

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.

SwiftでObjective-Cのインスタンス生成メソッドobjectWithXXXの置き換え

Last updated at Posted at 2014-07-06

内容

Objective-Cでインスタンスを生成するobjectWithXXXのようなクラスメソッドはSwiftだとClass(XXX:XXX)となる。
これは自作クラスでも適用される模様。

NSDateの例

NSDatedateWithTimeIntervalSince1970:の場合、それぞれ以下のようなコードになる。

Objective-C
NSDate * date = [NSDate dateWithTimeIntervalSince1970:0];
Swift
let date:NSDate = NSDate(timeIntervalSince1970:0)

自作クラスの例

Objective-Cで自作したクラスのクラスメソッドでも同様の対応は行われる。
TestClass+(TestClass*)testClassWithData:(TestData*)dataを用意すると以下のようになる。

Objective-C
TestClass * test = [TestClass testClassWithData:data];
Swift
let test:TestClass = TestClass(data:data)
9
8
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
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?