Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Sebastian Nitu
Sebastian Nitu
8,616 Points

Can anyone help with some suggestions on refactoring my Data Model?

Hello!

Could anyone help me with the issue described in this post of mine from Stack Overflow?

https://stackoverflow.com/questions/48250938/return-more-concrete-objects-from-data-model-in-swift

It’s been bugging me for over a week now and I am all freshly out of ideas.

Any suggestion is more than welcome!

Thanks in advance for the help!

1 Answer

Tom Makedonski
Tom Makedonski
439 Points

Yes, I answered your question on Stack Overflow, but here it is again:

I have some suggestions. It looks you keep using conditional binding to unwrap the snapshsot.value while casting it as an NSDictionary. Since unwrapping this value is prerequisite to unwrapping the other values, why not just use a guard statement to unwrap it? In your guard statement, you can then default initialize all the properties in your struct. Alternatively, if you are not adamant about default initializing your properties, you can just use a failable initializer and just return nil in the guard statement.

guard let snapshotDictionary = snapshot.value as? NSDictionary else { 
    title = "Cannot display Title for this item :("
    body = "Cannot display Body for this item :("
    summary = "Due to some weird error, the Summary for this item cannot be 
    displayed. Insert more coffee and Pizza in developer"
    ...
    ...
}