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

Implementing decode for a 2-D array #149

Closed
AndrewSB opened this issue Sep 13, 2016 · 4 comments
Closed

Implementing decode for a 2-D array #149

AndrewSB opened this issue Sep 13, 2016 · 4 comments
Labels

Comments

@AndrewSB
Copy link

AndrewSB commented Sep 13, 2016

I have an object Song

struct Song {
    ...
    let frequencies: [[Int]]
}

and right now I'm parsing it so:

guard let arrayOfFrequencies = e.rawValue.valueForKeyPath("frequencies") as? [AnyObject] else {
    throw customError("frequency parsing failed")
}       
guard let twoD = arrayOfFrequencies.map({ $0 as? [Int] }) as? [[Int]] else {        
    throw customError("frequency parsing failed")
}       

Is this the way I should be decoding it? Can I use Himotoki to make my life easier?

@ikesyo
Copy link
Owner

ikesyo commented Sep 15, 2016

How about this?

(e <|| "frequencies").map { (e: Extractor) throws -> [Int] in
    return try [Int].decode(e.rawValue)
}

@AndrewSB
Copy link
Author

@ikesyo this looks great to me 🎉
I'll test it out soon and close this if it works 😄

@ikesyo
Copy link
Owner

ikesyo commented Sep 27, 2016

@AndrewSB Any updates on this?

@AndrewSB
Copy link
Author

yup! this was perfect. Thanks @ikesyo

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

No branches or pull requests

2 participants