Skip to content

Best way to deserialize optional elements... #247

Answered by drmohundro
woolie asked this question in Q&A
Discussion options

You must be logged in to vote

Any reason you're not using the tie: node["Tie"].value() there like below? The deserializer can handle optionals like this (if the left side is optional anyway):

struct Note: XMLIndexerDeserializable {
	var id: Int
	var tie: Tie?
	var instrumentArticulation: Int
	var properties: [NoteProperty]

	static func deserialize(_ node: XMLIndexer) throws -> Self {
		return try Note(
			id: node.value(ofAttribute: "id"),
			tie: node["Tie"].value(),
			instrumentArticulation: node["InstrumentArticulation"].value(),
			properties: node["Properties"]["Property"].value()
		)
	}
}

Disclaimer: I haven't tried running this code, so it might have some issues...

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by woolie
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants