I can print only 1 data, not any more. This is my error:

Thread 1: Fatal error: Index out of range

This is my JSON:

[
    {
        "Guides": [
            {
                "_id": "5cbc780edfdb6307006aec37",
                "Text": "He is one of Soroush Friend",
                "Tavernier": 2
            },
            {
                "_id": "5cbc781bdfdb6307006aec38",
                "Text": "He is one of Soroush Friend",
                "Tavernier": 2
            }
        ]
     }
]

And this is my struct that works well:

struct GuideStruct: Codable {
    let guides: [Guide]
    enum CodingKeys: String, CodingKey {
        case guides = "Guides"
    }
}

struct Guide: Codable {
    let id, text: String
    let tavernier: Int
    enum CodingKeys: String, CodingKey {
        case id = "_id"
        case text = "Text"
        case tavernier = "Tavernier"
    }
}

And this is my array and my class:

internal static var guides = [guidesarr]()

class guidesarr {
    var _id : String
    var Text : String
    var Tavernier : Int
    init(_id : String,Text : String,Tavernier : Int) {
        self._id = _id
        self.Text = Text
        self.Tavernier = Tavernier
    }
}

And my codes in viewcontroller:

class GameViewController: UIViewController,UITextFieldDelegate {
    typealias guide1 = [GuideStruct]
    var i1 = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        let  headers : HTTPHeaders = ["Content-Type":"application/json","OAtcp":"0!QSJ5SDG8Q39PPM$DXP5HD1E10"]
        Alamofire.request("http://192.168.1.100:3535/DarkDiamonds/Api/GetActiveGames",method :.post,headers: headers).responseJSON { (newresponse) in
            do {
                let decoder = JSONDecoder()
                let responseguide = try decoder.decode(guide1.self, from: newresponse.data!)
                for each1 in responseguide {
                    let newstruct = guidesarr(_id:each1.guides[self.i1].id , Text: each1.guides[self.i1].text,                     Tavernier: each1.guides[self.i1].tavernier)
                    self.i1 = self.i1 + 1
                    AppDelegate.guides.append(newstruct)
                }
                print(AppDelegate.guides[0])
                print(AppDelegate.guides[1])
                print(AppDelegate.Games.count)
                print(AppDelegate.guides[0].Text)
                print(AppDelegate.guides[1].Text)
            }catch {

            }
        }
    }
}

I can print:

print(AppDelegate.guides[0])

And print this:

print(AppDelegate.guides[0].Text)

But when I want to print this:

print(AppDelegate.guides[1])
print(AppDelegate.guides[1].Text)

There is error:

Thread 1: Fatal error: Index out of range

1 Answers

1
rmaddy On Best Solutions

There are several issues in your code.

  1. The guidesarr class is unnecessary. Just use your Guide struct.
  2. Use proper naming conventions. Class, struct, and enum names should start with uppercase letters. Property, function, and case names should start with lowercase letters.
  3. Don't force-unwrap data. Safely check it and do proper error checking.
  4. Your main issue is that the two chunks of data you seem to actually want are the two Guide instances inside the one (not two) GuideStruct instances.

I would redo your code something like this:

class GameViewController: UIViewController,UITextFieldDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        let headers : HTTPHeaders = ["Content-Type":"application/json","OAtcp":"0!QSJ5SDG8Q39PPM$DXP5HD1E10"]
        Alamofire.request("http://192.168.1.100:3535/DarkDiamonds/Api/GetActiveGames", method: .post, headers: headers).responseJSON { (newresponse) in
            if let data = newresponse.data {
                do {
                    let decoder = JSONDecoder()
                    let guides = try decoder.decode([GuideStruct].self, from: data)
                    for guideStruct in guides {
                        AppDelegate.guides.append(contentsOf: guideStruct.guides)
                    }
                    print(AppDelegate.guides[0])
                    print(AppDelegate.guides[1])
                    print(AppDelegate.Games.count)
                    print(AppDelegate.guides[0].Text)
                    print(AppDelegate.guides[1].Text)
                }catch {
                    // Bad JSON
                }
            } else {
                // No data
            }
        }
    }
}

And change:

internal static var guides = [guidesarr]()

to:

internal static var guides = [Guide]()

And delete the guidearr class.