Why is an object not being decoded?
according to your JSON - "type":"GET_HORSE"
- you should change enum MessageType : Int,Decodable
to enum MessageType: String, Decodable
. I've checked your code after changes to a String
and got correct model here
if let json = try? decoder.decode(Message<Horse>.self, from: data!) {
print(json)
print("something")
}
but it's not json
, it's a model with Message<Horse>
type
JSON Decoder cannot decode an object into an object
What did you expect to happen / get printed?
The log
package (which uses the fmt
package) prints structs enclosed in braces, listing field values separated by spaces.
Your MiddleMan
has a single field, so it'll look like this:
{field}
Where field
is another struct
of type CompletedByUser
, which has 2 fields, so it'll look like this:
{{field1 field2}}
Where field
is of string
type, being the empty string, so you'll see the value of field2
prefixed with a space:
{{ a03dfee5-a754-4eb9}}
If you print it adding field names:
log.Printf("%+v", middleMan)
You'll see an output like:
{User:{DisplayName: Id:a03dfee5-a754-4eb9}}
Using another (Go-syntax) format:
log.Printf("%#v", middleMan)
Output:
main.MiddleMan{User:main.CompletedByUser{DisplayName:"", Id:"a03dfee5-a754-4eb9"}}
Try this on the Go Playground.
Why is my object of type SKTileMapNode not being decoded?
For anyone who runs into this issue, I spent the better part of a day figuring it out and found no luck. However, I figured out that the larger issue was found in my SKScene. My custom class was encoded and decoded as part of the scene. The solution was to use NSKeyedArchiver
on my scene as follows:
override func encodeRestorableState(with coder: NSCoder) {
self.sceneWasSaved = true
if self.gameState != nil {
coder.encode(self.sceneWasSaved, forKey: "GameViewController_sceneWasSaved")
coder.encode(self.gameState!, forKey: "GameViewController_gameState")
NSKeyedArchiver.archiveRootObject(self.gameScene!, toFile: filePath)
}
super.encodeRestorableState(with: coder)
}
Then unarchive:
vc.gameScene = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? GameScene
It seems that if we use the coder
and decoder
that Spritekit
types are not properly handled for whatever odd reason. But if you use the archiver method it handles them and both my SKScene
and my custom class was properly encoded and decoded.
Note I changed nothing in my custom class described in the question, only the archiving of SKScene
.
Anyway, I don't have an answer of why this works, but maybe it will save someone!
str' object has no attribute 'decode'. Python 3 error?
You are trying to decode an object that is already decoded. You have a str
, there is no need to decode from UTF-8 anymore.
Simply drop the .decode('utf-8')
part:
header_data = data[1][0][1]
As for your fetch()
call, you are explicitly asking for just the first message. Use a range if you want to retrieve more messages. See the documentation:
The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number (
'1'
), a range of message numbers ('2:4'
), or a group of non-contiguous ranges separated by commas ('1:3,6:9'
). A range can contain an asterisk to indicate an infinite upper bound ('3:*'
).
why Decode method in swift4 gives object nil if there is one tag in json payload has type mismatch
Please read the error message carefully, the reason is very clear (no need to guess)
Expected String but Int has been found
means the (found) value is Int
but you declared a String
property
All strings in JSON are wrapped in double quotes, the type for type
is Int
class Animal: Codable {
var name: String?
var type: Int?
}
If the JSON contains always both values declare the properties as non-optional by removing the question marks.
The implicit decoder / initializer fails if any error occurs, you can see this just from the code syntax. If you want to have finer control write your own custom initializer.
Related Topics
How to Create a Multi Line Text Field in Swiftui for MACos
Injecting a New Stylesheet into a Website via Uiwebview Using iOS8 Swift Xcode 6
Find Multiple Quoted Words in a String with Regex
How to Check If a Property Has Been Set Using Swift Reflection
How to Generate a Binding for Each Array Element
How to Distinguish Between Multiple Uipickerviews on One Page
Uitextview Font to Always Be Fixed Size
Swift 5 Table View Cell with Uiimage Appears Very Tall and Image Extremely Zoomed
How to Make Swiftui Listmenu with Different Behaviors
Programatic Constraints Not Obeyed
How to Cast an Any Value with Nil in It to a Any
Many Ways of Defining a Swift Dictionary
Toggle Selection in a List - Swiftui
Calling a Function Defined in Assembly from Swift
Get Lat and Long from Tapped Overlay in Google Maps
Nothing Prints Out in the Console in Command Line Tool Xcode
Timestamped Event Matching Error: Failed to Find Matching Element
Why Is Generic Specialization Lost Inside a Generic Function