如何在 Swift 中正确获取范围外的值 Do-Catch(使用 Try)
How Properly to Get the Value Outside of the Scope Do-Catch (using Try) in Swift
我正在尝试将 JSON 数据解析到字典中,为了解析我正在使用单独的方法,稍后想将结果(字典)用于另一种方法中的其他操作,而不仅仅是打印出来,因为它在许多在线示例中给出,例如。 G。 .
但是,我不能 return 这个值,因为我被要求在 guard 中插入 return 语句, 但插入后得到 "Non-void function should return a value".
代码如下所示:
func extractJSONDictionaryFrom(JSONData:NSData) ->NSMutableDictionary
{
var dict = NSMutableDictionary()
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return
}
dict = NSMutableDictionary(dictionary: JSON)
}
catch let JSONError as NSError {
print("\(JSONError)")
}
print("The JSON is \(dict)")
return dict
}
The approach 使用 throw 也几乎没有用,因为我需要在调用 "extractJSONDictionaryFrom"
时处理其他方法中的抛出
一个选项是让您的方法抛出错误(无论是 NSJSONSerialization
错误,因为 JSON 解析完全失败,还是您的自定义错误,如果 JSON 解析有效,但它不是'由于某种原因没有字典):
func extractJSONDictionaryFrom(JSONData: NSData) throws -> NSMutableDictionary {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:[]) as? NSDictionary else {
throw NSError(domain: NSBundle.mainBundle().bundleIdentifier!, code: -1, userInfo: [NSLocalizedDescriptionKey : "Not a dictionary"])
}
return NSMutableDictionary(dictionary: JSON)
}
或者,另一种方法是不让它抛出错误,而只是 return nil
如果转换失败:
func extractJSONDictionaryFrom(JSONData: NSData) -> NSMutableDictionary? {
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return nil
}
return NSMutableDictionary(dictionary: JSON)
} catch let JSONError as NSError {
print("\(JSONError)")
return nil
}
}
我倾向于前者,但后者也行。
将结果设为可选 (NSMutableDictionary?
) 并使用 return nil
.
如果调用者不应更改 returned 词典,您可能还想 return NSDictionary?
。
编辑
给定的代码不起作用,因为 NSJSONSerialization.JSONObjectWithData
抛出错误 JSON,这被 catch
捕获。函数 return 是一个空字典而不是 nil
。我会尝试以下操作:
func extractJSONDictionaryFrom(JSONData:NSData) ->NSMutableDictionary?
{
var dict = NSMutableDictionary()
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return nil
}
dict = NSMutableDictionary(dictionary: JSON)
}
catch let JSONError as NSError {
print("\(JSONError)")
return nil
}
print("The JSON is \(dict)")
return dict
}
我也认为传播异常会更好。
或者,您可以 return 所有错误的空字典而不使用可选结果,但是这个 "hides" 错误可能不是一个好主意。
我正在尝试将 JSON 数据解析到字典中,为了解析我正在使用单独的方法,稍后想将结果(字典)用于另一种方法中的其他操作,而不仅仅是打印出来,因为它在许多在线示例中给出,例如。 G。
但是,我不能 return 这个值,因为我被要求在 guard 中插入 return 语句, 但插入后得到 "Non-void function should return a value".
代码如下所示:
func extractJSONDictionaryFrom(JSONData:NSData) ->NSMutableDictionary
{
var dict = NSMutableDictionary()
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return
}
dict = NSMutableDictionary(dictionary: JSON)
}
catch let JSONError as NSError {
print("\(JSONError)")
}
print("The JSON is \(dict)")
return dict
}
The approach 使用 throw 也几乎没有用,因为我需要在调用 "extractJSONDictionaryFrom"
时处理其他方法中的抛出一个选项是让您的方法抛出错误(无论是 NSJSONSerialization
错误,因为 JSON 解析完全失败,还是您的自定义错误,如果 JSON 解析有效,但它不是'由于某种原因没有字典):
func extractJSONDictionaryFrom(JSONData: NSData) throws -> NSMutableDictionary {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:[]) as? NSDictionary else {
throw NSError(domain: NSBundle.mainBundle().bundleIdentifier!, code: -1, userInfo: [NSLocalizedDescriptionKey : "Not a dictionary"])
}
return NSMutableDictionary(dictionary: JSON)
}
或者,另一种方法是不让它抛出错误,而只是 return nil
如果转换失败:
func extractJSONDictionaryFrom(JSONData: NSData) -> NSMutableDictionary? {
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return nil
}
return NSMutableDictionary(dictionary: JSON)
} catch let JSONError as NSError {
print("\(JSONError)")
return nil
}
}
我倾向于前者,但后者也行。
将结果设为可选 (NSMutableDictionary?
) 并使用 return nil
.
如果调用者不应更改 returned 词典,您可能还想 return NSDictionary?
。
编辑
给定的代码不起作用,因为 NSJSONSerialization.JSONObjectWithData
抛出错误 JSON,这被 catch
捕获。函数 return 是一个空字典而不是 nil
。我会尝试以下操作:
func extractJSONDictionaryFrom(JSONData:NSData) ->NSMutableDictionary?
{
var dict = NSMutableDictionary()
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return nil
}
dict = NSMutableDictionary(dictionary: JSON)
}
catch let JSONError as NSError {
print("\(JSONError)")
return nil
}
print("The JSON is \(dict)")
return dict
}
我也认为传播异常会更好。
或者,您可以 return 所有错误的空字典而不使用可选结果,但是这个 "hides" 错误可能不是一个好主意。