Understanding the "Dictionary Type" Return Value in Swift

Asked 1 years ago, Updated 1 years ago, 67 views

 func getJson (param:models) - > Dictionary {
    let url="https://test.com/"+models+".php"
    let json = JSON(data:super.HttpRequest())
    print(json.type) / * print result "Dictionary" */
    The contents of print(json)/*json have returned the intended data.*/
    print(json['name'])/*There is no problem with Taro Yamada as specified on the left.*/
    return json
}
/* call*/
myClass.getJson()

I run httpRequest and create a program to receive json data.

if the return data type is "Dictionary" You get the error Reference to generic type 'Dictionary' requirements arguments in <...>.
Even if the type of the "json" variable received from the JSON function is "printed", it becomes "Dictionary", but the return value of "->Dictionary" is an error.

Would it be possible for you?Thank you for your cooperation.

swift xcode

2022-09-30 20:59

1 Answers

The error message states that Dictionary must be a generic type specification (for example, Dictionary<String, AnyObject>) to return the function, but it does not matter here.

The type information returned by the type property simply indicates the type of JSON root element, and the type on Swift is JSON.

 func getJson (param:models) - > JSON {
    let url="https://test.com/"+models+".php"
    let json = JSON(data:super.HttpRequest())
    return json
}


2022-09-30 20:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.