为什么在类中初始化字典数组时内存泄漏?

时间:2017-01-02 08:26:50

标签: swift dictionary memory-leaks

这是我的课程初始化程序:

typealias JSONDictionaty = [String: AnyObject]
private var _conditions: [[String: String]]?

init(data: JSONDictionaty) {
    if let conditions = data["conditions"] as? [JSONDictionaty] {
        if conditions.count > 0 {
            self._conditions = [[:]] // init _condition
        }

        // adding all conditions to _condition variable
        for condition in conditions {
            if let subCond = condition as? [String: String] ,
                   type = subCond["type"], 
                   expression = subCond["expression"] {
                       self._conditions?.append([type: expression])
                }
            }
        }

        /// deinit _condition variable 
        if self._conditions?.count == 0 {
            self._conditions = nil
        }
    }
}

我有这方面的漏洞:

if let type = subCond["type"], expression = subCond["expression"] {
       self._conditions?.append([type: expression])
} 

但如果我将上面的行更改为此行,则内存泄漏将会消失:

self._conditions?.append(subCond)

0 个答案:

没有答案