LoginSignup
3
3

More than 5 years have passed since last update.

Swiftでネストした構造体の初期化エラー

Last updated at Posted at 2017-03-25

概要

下記のようにネストした構造体の配列を初期化しようとした時に何故かコンパイルエラーが出たのでメモ。
(Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1))

ネストした構造体配列の初期化エラー

構造体StructAの中に構造体StructBを定義し、入れ子にする。

ネストした構造体
struct StructA {
    let a: Int

    struct StructB {
        let b: Int
    }

}

上記のStructBの配列を初期化しようとしたときに、
以下の記述だと何故かシンタックスシュガーが機能せず、コンパイルエラーが出る。

StructBの配列を初期化
// NGパターン
let list1 = [StructA.StructB]()
// error: cannot call value of non-function type '[StructA.StructB.Type]'

回避方法

以下記述方法であればコンパイルエラーはでない。

回避方法
// OKパターン
let list2 = Array<StructA.StructB>()

let list3: [StructA.StructB] = []

typealias StructBinA = StructA.StructB
let list4 = [StructBinA]()

参考

stackoverflowを見るとどうやらswiftのバグらしい。
- stackoverflow / Array of Nested Type: Why Does the Compiler Complain?
- stackoverflow / Why can't I instantiate an empty array of a nested class?

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