2017-11-20 2 views
1

SwiftyJSONのすべてのswitch文をif-else条件に変換すると、switch文がメモリリークを起こしているためです。Swift内の他の場合にスイッチを変換する

私はほとんどすべてのスイッチ・ステートメントを変換しましたが、私はこれで立ち往生腸:

fileprivate subscript(sub sub: JSONSubscriptType) -> JSON { 
    ... 
    switch sub.jsonKey { 
     case .index(let index): return self[index: index] 
     case .key(let key): return self[key: key] 
    } 
    ... 
} 

public enum JSONKey { 
    case index(Int) 
    case key(String) 
} 

誰かが私を助けてくださいことはできますか?事前に

タンク、 マイケル

答えて

3
switch sub.jsonKey { 
    case .index(let index): return self[index: index] 
    case .key(let key): return self[key: key] 
} 

if case .index(let index) = sub.jsonKey { 
    return self[index: index] 
} else if case .key(let key) = sub.jsonKey { 
    return self[key: key] 
} 

または抽象になります。

switch value { 
    case .a(let x): doFoo(x) 
    case .b(let y): doBar(y) 
} 

なり、
if case .a(let x) = value { 
    doFoo(x) 
} else if case .b(let y) = value { 
    doBar(y) 
} 
関連する問題