swift - Making a variable from if statement global -
while encoding json, i´m unwrapping stuff if let
statement, i'd make variable globally available
do { if let json = try jsonserialization.jsonobject(with: data) as? [string: string], let jsonisexistant = json["isexistant"] { // here make jsonisexistant globally available }
is possible? if isn't, make if
statement inside of one, don't think clever or possible.
delclare jsonisexistant @ place want it. if making ios app, above viewdidload()
create variable
var jsonisexistant: string?
then @ point use it
do { if let json = try jsonserialization.jsonobject(with: data) as? [string: string], let tempjsonisexistant = json["isexistant"] { jsonisexistant = tempjsonisexistant } }
this rewritten though
do { if let json = try jsonserialization.jsonobject(with: data) as? [string: string] { jsonisexistant = json["isexistant"] } } catch { //handle error }
if handled second way, have check if jsonisexistant nil before use, or unwrap ! if sure have field "isexistant" every time succeeds @ becoming json.
Comments
Post a Comment