Swift - 未设置默认属性值

时间:2015-11-04 08:13:01

标签: ios objective-c iphone swift swift2

为什么不使用默认值初始化属性?如果我取消注释

implode("\n\n", array_column($rawSubjectsArray, 'campaign_subjects'));

implode("<br/><br/>", array_column($rawSubjectsArray, 'campaign_subjects'));

一切正常。

enter image description here

更新

看起来问题与泛型类型&#34;元素&#34;有关,以下代码按预期工作:

//    required init?(coder aDecoder: NSCoder)
//    {
//        super.init(coder: aDecoder)
//    }

2 个答案:

答案 0 :(得分:0)

  

首先如此删除,你只需添加(或)初始化数据

 var sections: [[Element]]?

答案 1 :(得分:0)

// simplified example of your trouble

class C<T> {
    var arr: [T] = []
    func f() {

        // (1) it seems that the type of the container is exactly the same,
        //     as the type of the arr defined in step (3)

        print(arr.dynamicType)  // Array<Int>
        print(T.self)           // Int
        /*
        so far, so good ..
        can our arr collect integer numbers ????
        */

        //arr.append(1)       // error: cannot convert value of type '[Array<Int>]' to expected argument type '[_]'

        print(arr)              // []

        // why?
        // see the propper use of generic class at (4)
    }
}

let c = C<Int>()                // C<Int>
c.f()

// (2)
print(Int.self)                 // Int
var arr = [Int]()               // []

// (3)
print(arr.dynamicType)          // Array<Int>
arr.append(2)                   // [2]

// (4)
c.arr.append(1)
c.arr.append(2)
c.f()                           // [1, 2]
print(c)                        // C<Swift.Int>

let d = C<String>()
//c.arr.append("alfa")            // error: Cannot convert value of type 'String' to expected argument type 'Int'
d.arr.append("alfa")
print(d)                        // C<Swift.String>