Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force XML List? #274

Open
lpbas opened this issue Mar 26, 2018 · 4 comments
Open

Force XML List? #274

lpbas opened this issue Mar 26, 2018 · 4 comments

Comments

@lpbas
Copy link

lpbas commented Mar 26, 2018

First of all, thank you for this library, it has made parsing huge feeds a pleasure!

I would like to ask, if it's possible to force a value to be parced as a list. My data is in XML format, and I have a field "games" which contains a list of rounds, which, in turn contains a list of games like the following:

<games>
    <round code="1">
        <game .../>
        <game .../>
        <game .../>
    </round>
    <round code="2">
        <game .../>
        <game .../>
    </round>
</games>

The problem is that if my dataset contains only one round, then EVReflection cannot parse the list as a list. Instead i get the warning WARNING: The class 'Round' is not key value coding-compliant for the key 'round'

How can I force the mapping of the rounds as a list, even if there is only one round? Thank you very much!

@evermeer
Copy link
Owner

So your response has an array of rounds and each round can have an array of games. I think your class definition should then look something like this:

class XmlResponse: EVObject {
    var __name: String?
    var round: [Round]?
}
class Round: EVObject {
    var __name: String?
    var _code: String?
    var game: [Game]?
}
class Game: EVObject {
    var name: String?
    var something: String?
}

if there is only one round, then you will have a round array with only one element.

@lpbas
Copy link
Author

lpbas commented Mar 27, 2018

Thank you for the reply!

I also have another top level object, the Team, which makes it hard to parse the "games" so I should have included this in my original post. My data looks like this:

<teams>
	<team code="...">
		<name>...</name>
		... (other fields)
		<games>
		    <round code="1">
		        <game .../>
		        <game .../>
		        <game .../>
		    </round>
		    <round code="2">
		        <game .../>
		        <game .../>
		    </round>
		</games>
	</team>
	<team code="... (another team)">
		...
	</team>
</teams>

and there will be multiple teams. I can parse the fields but I fail to parse the games. How should my Team object look like?

Currently it looks like this:

class Team: EVObject {
	var _code: String = ""
	var name: String = ""
	//other properties

	var round: [Round]?
}

However, with this "Team" class, i get the WARNING: The class 'Team' is not key value coding-compliant for the key 'games' There is no support for optional type, array of optionals or enum properties.
How do I approach this?

@evermeer
Copy link
Owner

evermeer commented Mar 27, 2018

because your team has multiple properties it expect games as a property.
Since your games node only has a list of round you could create a property like this:
var games: [Round]?

If you games node had other values besides round, then you would have needed an extra object for storing those values. In this case EVReflection assumes that the list could be used.

if you would like to have your games property named round, then you could create a propertymapping for that.

@lpbas
Copy link
Author

lpbas commented Mar 28, 2018

Thank you for this. Now my "Round" object cannot parse the rounds if there is only one round... I get the following warning:

WARNING: The class 'Round' is not key value coding-compliant for the key 'round' where my Round class is the following:

class Round: EVObject {
	var _code: String = ""
	//... (other string properties)

	var game: [Game]?
}

If there are two or more rounds, the the object gets parsed sucessfully.
Is this a bug or am I doing somehting wrong with how I define my lists? (That's why I aksed if its possible to force a property as a list, since the parsing fails when there is only one round, and I need my objects to work even with an one element array)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants