0

I have a RestAPI that pulls JSON data from a webpage, and because I want to follow MVC programming standards, I want to fetch this data through a "Data List" class that instantiates the RestAPI Class.

I have a class called "Card", which provides the basic structure of a type of data object. I have a class called "CardList" whose initialization creates an Array of the "Card" Classes.

However, I'm having trouble appending Card Class data to my CardList Array.

Here is the code for CardList Class:

class CardList {

    var cards: [Card]

    static var sharedInstance = CardList()

    //MARK - Initalize
    private init(){

        //Dummy Data
        let helm = Card(name: "Helm of Testing", cost: 10, type: "Equipment", subType: "Head Armor", description: "Some say the Helmet of Testing helps keep it's wearers mind clear.")

        //Add to Array
        var c = [helm]
        let items: NSMutableArray = []

        //Get API Array
        RestApiManager.sharedInstance.getElements { (json: JSON) in
            let results = json["results"]
            for (_, subJson) in results {
                let card:AnyObject = subJson["card"].object
                items.addObject(card)
            }

            for var i = 0; i < items.count; ++i{
                let data:JSON = JSON(items[i])
                let newCard = Card(name: data["title"].stringValue, cost: Int(data["cost"].string!)!, type: data["type_id"].stringValue, subType: data["subtype_id"].stringValue, description: data["description"].stringValue)

                c.append(newCard)
            }

        }
        cards = c.sort { $0.cost < $1.cost }
    }

    //END Class
}

So basically on initialization, this class creates a dummy Card, adds it to an array called "c". An array called "items" is created. There RestApiManager returns JSON data, for every type of data returned in the results, it gets added into the "items" array.

We then loop through the "items" array, reading it as JSON (I'm using the SwiftyJSON plugin/code snip) and creating a new "Card" Class for each item in the array. We then append the "Card" to the "c" Array.

Finally we take the Card Array "cards" and set it equal to the "c" Array sorted by it's value cost.

The Problem:

After running the code, the "cards" Array only returns the dummy card named "helm", and none of the data added to the "c" array.

Notes:

Yes, my Api is working, if I print the values in the "for var i = 0" loop, it is printing correct values. If I print the "c.count" in the same loop, I get two (The API is only returning 1 other data set, or "Card"). If I print the "c.count" outside of that loop, after the loop, it says there is only 1 item in the Array when there should be two (the dummy data and the data returned from the JSON call).

So, it's clearly something with my syntax. Or maybe I'm making things more complicated than they need to. I'm fairly new to Swift and slightly confused. I just need someone else to look at my code.

Thank you in advance.

1 Answer 1

1

RestApiManager.sharedInstance.getElements works asynchronously.
The block containing the data is executed after the init function exits.

Move the code to assign and sort the array into the block.

...
        for var i = 0; i < items.count; ++i{
            let data:JSON = JSON(items[i])
            let newCard = Card(name: data["title"].stringValue, cost: Int(data["cost"].string!)!, type: data["type_id"].stringValue, subType: data["subtype_id"].stringValue, description: data["description"].stringValue)

            c.append(newCard)
        }
        cards = c.sort { $0.cost < $1.cost }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

If i move the code, It give me an error "requires explicit 'self.', so I put the self.cards = c.sort { $0.cost < $1.cost } in the block. But now it errors out saying "Variable 'self.cards' used before being initialized". Ideas?
Declare cards as an empty array : var cards: [Card]()
App table view is returning no data now, array is empy :/
im using var cards: [Card] = [], but even with yours, it's still empty on initilize. I added a print(self.cards.count) after the self.cards = c.sort { $0.cost < $1.cost } and it returns with a value of 2.. yet no data is shown, truly at a loss
Maybe I need to reload the table data somehow?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.