Search code examples
iosarraysswiftalamofire

indexout of range error while getting image array


Hello i am getting images array from response but i am getting whole array but some time i am getting 1 image and sometime 5 image so when i am populating image view i am getting index out of range error let me show you response

Model Class

struct PreviousInspListModel {
    var resolved: String
    var type: String
    var notes: String
    var property_id: String
    var regulation_item: String
    var locations: String
    var id: String
    var boiler_service_date: String
    var action: String
    var intervention_required: String
    var regulation_id: String
    var regulation_item_name: String
    var contravention: String
    var contraventions_item_name: String
    var imagesArray : [String]!
}

Response

[RentComply.PreviousInspListModel(resolved: "N/A", type: "Internal", notes: "TEst", property_id: "22", regulation_item: "8", locations: "Test", id: "64", boiler_service_date: "", action: "Further Investigation / Maintenance Required", intervention_required: "N/A", regulation_id: "4", regulation_item_name: "Floors", contravention: "27", contraventions_item_name: "Damaged wooden floors", imagesArray: Optional(["http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_5fzy77gNL31548237434.png"]))]



[RentComply.PreviousInspListModel(resolved: "N/A", type: "Internal", notes: "TEst", property_id: "22", regulation_item: "8", locations: "Test", id: "64", boiler_service_date: "", action: "Further Investigation / Maintenance Required", intervention_required: "N/A", regulation_id: "4", regulation_item_name: "Floors", contravention: "27", contraventions_item_name: "Damaged wooden floors", imagesArray: Optional(["http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_5fzy77gNL31548237434.png"])), RentComply.PreviousInspListModel(resolved: "N/A", type: "Internal", notes: "dsds", property_id: "22", regulation_item: "8", locations: "asdsad", id: "65", boiler_service_date: "", action: "Maintenance Attention Required", intervention_required: "N/A", regulation_id: "4", regulation_item_name: "Floors", contravention: "27", contraventions_item_name: "Damaged wooden floors", imagesArray: Optional(["http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_5aQueNfpUp1548247477.png", "http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_8dlP5RlUqS1548247477.png", "http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_fqqqC9zfe91548247477.png", "http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_sjkjOy73Y11548247477.png", "http://christies.buildingcontrolregister.ie/webservice/assets/uploads/inspection_photo/img_t7ePWkHif71548247477.png"]))]

as i show you for first response i am only getting one image and in second i am getting 5 images now i will show you my code

Code

var preInspData = [PreviousInspListModel]()

func previousInspAPI(){
    let preferences = UserDefaults.standard
    let uid = "u_id"
    let acTkn = "acc_tkn"

    let u_ID = preferences.object(forKey: uid)
    let A_Token = preferences.object(forKey: acTkn)

    let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!]
    print(params)
    SVProgressHUD.show()
    Alamofire.request(previousinspectionslist, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
        switch response.result{
        case.success(let value):
            let json  = JSON(value)
            print(json)
            let data = json["data"]

            data.array?.forEach({ (iList) in
                let i_List = PreviousInspListModel(resolved: iList["resolved"].stringValue, type: iList["type"].stringValue, notes: iList["notes"].stringValue, property_id: iList["property_id"].stringValue, regulation_item: iList["regulation_item"].stringValue, locations: iList["locations"].stringValue, id: iList["id"].stringValue, boiler_service_date: iList["boiler_service_date"].stringValue, action: iList["action"].stringValue, intervention_required: iList["intervention_required"].stringValue, regulation_id: iList["regulation_id"].stringValue, regulation_item_name: iList["regulation_item_name"].stringValue, contravention: iList["contravention"].stringValue, contraventions_item_name: iList["contraventions_item_name"].stringValue, imagesArray: iList["images"].arrayObject as? [String])
                self.preInspData.append(i_List)
                print(self.preInspData)
            })
            self.tblListView.reloadData()
            SVProgressHUD.dismiss()
        case.failure(let error):
            print(error.localizedDescription)
        }

    })
}

   func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return preInspData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! PreviousInspectionsTableViewCell
        cell.lblItem.text = preInspData[indexPath.row].regulation_item_name
        cell.lblContrevation.text = preInspData[indexPath.row].contraventions_item_name
        cell.lblLocation.text = preInspData[indexPath.row].locations
        cell.lblAction.text = preInspData[indexPath.row].action
        cell.lblNotes.text = preInspData[indexPath.row].notes
        cell.lblStatus.text = preInspData[indexPath.row].intervention_required

        let images = preInspData[indexPath.row].imagesArray

        let image1 = images![0]
        if image1 != ""{
            let imageUrl = URL(string: image1)!
            cell.img1.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
        }

        let image2 = images![1]
        if image2 != ""{
            let imageUrl = URL(string: image2)!
            cell.img2.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
        }

        let image3 = images![2]
        if image3 != ""{
            let imageUrl = URL(string: image3)!
            cell.img3.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
        }

        let image4 = images![3]
        if image4 != ""{
            let imageUrl = URL(string: image4)!
            cell.img4.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
        }

        let image5 = images![4]
        if image5 != ""{
            let imageUrl = URL(string: image5)!
            cell.img5.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
        }

        cell.selectionStyle = .none
        return cell
    }

so now if there are only one images in array so i will get index out of range error so how to handle this please help me


Solution

  • The problem is about getting an element from an array whose index does not exist. i.e Index out of range

    Your cell for row code is causing an issue :

    let image1 = images![0]
    if image1 != ""{
        let imageUrl = URL(string: image1)!
        cell.img1.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
    }
    

    Solution to this problem is handling index out range error by adding this extension method:

     extension Collection where Indices.Iterator.Element == Index {
        subscript (safe index: Index) -> Iterator.Element? {
            return indices.contains(index) ? self[index] : nil
        }
    }
    
    extension Array {
        subscript (safe index: Int) -> Element? {
            return indices ~= index ? self[index] : nil
        }
    }
    

    Usage:

    if let image1 = images[safe: 0], image1 != "" {
            let imageUrl = URL(string: image1)!
            cell.img1.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
    }