Swift “try” Memory LEAK

Swift has something called “try” which is the modern way to handle errors. If you use this you will have to wrap your code in a do catch block like so.

do {
let json = try NSJSONSerialization.JSONObjectWithData(someData, options: [])
} catch {}

It works as it should. We also have the ability to use “try?” Which means it will return nil when error happens. There is also try! When we know the error will never happen.

But there is a problem…

if let g = try? NSJSONSerialization.JSONObjectWithData(someData, options: []) {
}

Results in a memory leak

Therefore you should avoid “try?” Until Apple officially fixes this.

— —

Update 4–15–2016

On Xcode 7.3 I am unable to reproduce this memory leak.