Array类型当作泛型参数时,无法成功取值
- Dominant language
- Swift
- Stars
- 4.3k
- Forks
- 677
- PR merge metrics
- No merged PRs in 30d
Description
服务端结果可能是这样的,要使用泛型支持2种结果的解析
```
{
"data": [
{"key":"value"}
],
"succeeded": true,
"message": "",
"code": 123
}
{
"data": {
"key": "value"
},
"succeeded": true,
"message": "",
"code": 123
}
```
我的代码如下
https://gist.github.com/sunbeams001/a33e0af379077b1ffd6507a1b4fd243d
```
import HandyJSON
extension Array: HandyJSON {}
extension String: HandyJSON {}
extension NSDictionary: HandyJSON {}
class ResponseData: HandyJSON {
var code: Int = 0
var data: T?
var message: String?
var succeeded: Bool = false
required init() {}
func mapping(mapper: HelpingMapper) {
mapper <<<
data <-- ("data", TransformOf(
fromJSON: { rawString in
if T.self is Array.Type && (T.self as! Array.Type).Element.self is HandyJSON.Type {
return type(of: [(T.self as! Array.Type).Element.self as! HandyJSON.Type]).deserialize(from: rawString?.toJSONString()) as? T
} else {
return T.deserialize(from: rawString?.toJSONString())
}
}, toJSON: { value in
value??.toJSONString()
}))
}
}
```
结果
ResponseData 可以成功
ResponseData<[SomeModel]> 不行,解析时 rawString 为 nil
我看issue上有些也提到这个问题,这种的有解决方案吗
#113
#342
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the linked gist and the ResponseData.mapping implementation, focusing on the TransformOf conversion of the data field. Reproduce the difference between ResponseData and ResponseData<[SomeModel]> using the two response shapes and the referenced issues #113 and #342. Done means both object and array generic results deserialize successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100