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

Initializing nested arrays? #183

Closed
noobs2ninjas opened this issue Apr 7, 2017 · 15 comments
Closed

Initializing nested arrays? #183

noobs2ninjas opened this issue Apr 7, 2017 · 15 comments
Labels

Comments

@noobs2ninjas
Copy link

noobs2ninjas commented Apr 7, 2017

I am trying to find a way to initialize an array with nested arrays without having to initialize a class to do so.

I tried
let myArrays = [[[MyObject]]](json: myDataString)

But of course I got an error saying that [[MyObject]] was not a subclass of NSObject. (Subquestion...using this method it doesn't have to be a EVObject?)

I also get the same error doing it through classes.

class DataClass: EVObject {
    var myArrays = [[[[MyObject]]]()
}

also did

class DataClass: EVObject {
    var myArrays: Array<Array<Array<MyObject>>> = []
}

I get the same error every time. Not sure what to do to get this to work without making a property converter. Even if I went that route Im not sure what I would convert this into. Maybe a

Array<Array<Array< NSDictionary >>>

and try to loop through each array till I have my array of dictionaries and then convert that?

@evermeer
Copy link
Owner

Hi, I was away for a long weekend...

For your first question: NSObject is good enough in a lot of cases. If you do want 99% of the functionality, then you should extend it with the EVReflectable protocol. If you want to have all functionality, then it should be an EVObject.

And then your other question... Is your json structured like this?
[[{{\"field\",\"value\"},{\"field\",\"value2\"}]]

I will have a look how that should work.

@noobs2ninjas
Copy link
Author

Yes that is correct on the structure and all objects nested are a direct subclass of EVObject. I thought it may have been my fault so I tried the same structure but with simple NSNumbers as the data. Same result.

@evermeer
Copy link
Owner

This code does work:

    func testNestedArray(){
        let json = "{\"array\":[[{\"openId\":\"value1\"},{\"openId\":\"value2\"}],[{\"openId\":\"value3\"},{\"openId\":\"value4\"}]]}"
        let obj = A82(json: json)
        print(obj)
    }
}

class A82: EVObject {
    var array: [[A81]] = [[]]
}

class A81: EVObject {
    var openId: String = ""
}

@evermeer
Copy link
Owner

And this also works:

    func testNestedArray(){
        let json = "{\"array\":[[[{\"openId\":\"value1\"},{\"openId\":\"value2\"}],[{\"openId\":\"value3\"},{\"openId\":\"value4\"}]]]}"
        let obj = A82(json: json)
        print(obj)
    }
}

class A82: EVObject {
    var array: [[[A81]]] = [[[]]]
}

class A81: EVObject {
    var openId: String = ""
}

@noobs2ninjas
Copy link
Author

noobs2ninjas commented Apr 13, 2017

I'm so sorry. How dumb of me. Totally forgot to mention. My errors came when trying to iterate through the array. Or even pulling a object via index. array[0][0][0]

@noobs2ninjas
Copy link
Author

Heres the actual error.

fatal error: NSArray element failed to match the Swift Array Element type

This comes when looping as follows. It only crashes when looping through the objects themselves.

for row in object.array{
            for stack in row{
                for item in stack{
                    print(item.property)
                }
            }
}

This is what I get rather than my class objects when I breakpoint and inspect it.
[0] __NSDictionaryM * 3 key/value pairs 0x0000610000245400

@evermeer evermeer removed the fixed? label Apr 14, 2017
@evermeer
Copy link
Owner

I will have a look at it this weekend

@noobs2ninjas
Copy link
Author

Get a chance to look at it? Anyway I can help? If it seems like its going to be a little while before you get to it, I could just make a property converter. I'm sure you're a busy man. I can imagine what you do professionally if you create things like this on the side lol.

@evermeer
Copy link
Owner

I have confirmed it's a bug. It sets the value with the dictionary array. Strange enough you can just print it. That's why I assumed it was fixed. This week I was passed out with the flue, so I did not have a chance to see if there is a fix. I do know this one will be hard to fix. So don't expect a fix within 2 weeks. A property converter probably is the fastest solution. But still I do want this to be fixed.

@evermeer evermeer added the bug label Apr 20, 2017
@noobs2ninjas
Copy link
Author

Awesome! Thanks for letting me know! I had no doubt you would wanna get it fixed. You make this repository by far one of the most responsive I have ever seen when it comes to issues. Hope you are feeling better!

When it comes to the property converter. I assume it should be able to be to be converted into a [[[NSDictionary]]]? I think that's what I saw when I inspected it upon crash.

@evermeer
Copy link
Owner

I'm feeling better, thanks.
It's kind of my personal task to try to keep the number of issues as low as possible.
Yes, you will get a [[[NSDictionary]]] as the value in the propertyConverter function.

I already found out where I have to add the support for nested arrays. It's in the dictionaryAndArrayConversion function. I see dictionaryAndArrayConversion I have enough info to fix it there.

@evermeer
Copy link
Owner

I will push a fix for this issue later today. It's working.. I only have to create a couple of extra unit tests...

@evermeer
Copy link
Owner

Its pushed as version 4.11.0
You can find the tests here:
https://github.com/evermeer/EVReflection/blob/master/UnitTests/EVReflectionTests/EVReflectionTests.swift#L493

@noobs2ninjas
Copy link
Author

noobs2ninjas commented May 13, 2017

fatal error: NSArray element failed to match the Swift Array Element type

@evermeer Still have an issue when trying to enumerate. Haven't tried accessing individual objects in my application like you have in your test in a while.

Not sure if I mentioned it but these EVObjects inside the nested arrays contain other EVObjects. Haha even some of those objects inside those objects have EVObjects. Not sure if that can contribute. Haha we kinda used this kick ass framework as the backbone to initialize our entire model.

Not a huge deal to get this done though. I turned my json data into [[[NSDictionary]]] and just looped and initialized individual arrays of EVObjects. That works for now. Again though, I appreciate your hard work.

@noobs2ninjas
Copy link
Author

I tried pulling an individual object myData.data[0][0][0] and no luck

When I crash and inspect an individual object post-crash during the array loop I see

[0] __NSDictionaryM * 2 key/value pairs 0x0000600000244b30

When I look at its sub objects I see this.

[0] (null) "subobject" : 1 element

Not sure if any of this helps but thought id mention it.

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