swift4

Hot to decode JSON data that could and array or a single element in Swift?

早过忘川 提交于 2021-01-15 19:13:51
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [

Hot to decode JSON data that could and array or a single element in Swift?

丶灬走出姿态 提交于 2021-01-15 19:08:17
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [

Bool.hashValue valid to convert to Int?

对着背影说爱祢 提交于 2020-12-11 18:42:29
问题 In some cases and some code I saw that hashValue is used to convert Bool to Int . However, the code let someValue = true let someOtherValue = false print(someValue.hashValue) print(someOtherValue.hashValue) gets me the output -5519895559129191040 7814522403520016984 I would expect 1 and 0 , though. I use XCode 10.0 beta 2 (10L177m), MacOS High Sierra with Swift 4.2. I can switch to Swift 4.0 to gain likewise results. Now, is there something I do wrong or is hashValue no reliable conversion

Bool.hashValue valid to convert to Int?

馋奶兔 提交于 2020-12-11 18:20:37
问题 In some cases and some code I saw that hashValue is used to convert Bool to Int . However, the code let someValue = true let someOtherValue = false print(someValue.hashValue) print(someOtherValue.hashValue) gets me the output -5519895559129191040 7814522403520016984 I would expect 1 and 0 , though. I use XCode 10.0 beta 2 (10L177m), MacOS High Sierra with Swift 4.2. I can switch to Swift 4.0 to gain likewise results. Now, is there something I do wrong or is hashValue no reliable conversion