Skip to content

Commit 4e5fcff

Browse files
committed
use pattern matching
1 parent aeb52de commit 4e5fcff

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

Source/SocketPacket.swift

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,19 @@ struct SocketPacket {
195195
}
196196

197197
private mutating func _fillInPlaceholders(data: AnyObject) -> AnyObject {
198-
if let str = data as? String {
199-
if let num = str["~~(\\d)"].groups() {
200-
return binary[Int(num[1])!]
201-
} else {
202-
return str
203-
}
204-
} else if let dict = data as? NSDictionary {
205-
let newDict = NSMutableDictionary(dictionary: dict)
206-
207-
for (key, value) in dict {
208-
newDict[key as! NSCopying] = _fillInPlaceholders(value)
209-
}
210-
211-
return newDict
212-
} else if let arr = data as? [AnyObject] {
198+
switch data {
199+
case let string as String where string["~~(\\d)"].groups() != nil:
200+
return binary[Int(string["~~(\\d)"].groups()![1])!]
201+
case let dict as NSDictionary:
202+
return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in
203+
cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1)
204+
return cur
205+
})
206+
case let arr as [AnyObject]:
213207
return arr.map({_fillInPlaceholders($0)})
214-
} else {
208+
default:
215209
return data
210+
216211
}
217212
}
218213
}

0 commit comments

Comments
 (0)