-2

Wherever the designation is there those are extra i.e the default valueI am trying to get number of post done by one particular user. With my code below I am trying to match the value by UserDefaults in cellForAtIndexPath it's giving me all the values from the database:

import UIKit

struct OverViewJob: Decodable {
    let id: String
    let c_id: String
    let desig: String
    let exp_from: String
    let location: String
    let edu_qual: String
}

class OverviewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    var jobOverViewArray = [OverViewJob]()
    @IBOutlet weak var jobTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


getJobOverviewData()

        //MARK:: getting values by userdefaults

        let companyDefaultValue = UserDefaults.standard
        if let userID = companyDefaultValue.string(forKey: "user_type_id"){
            print("User type ID=\(userID)")
        }
    }

func getJobOverviewData()
        {
            let jobUrl = URL(string: "http://172.16.1.22/Get-Job-API/get-jobs/")
            URLSession.shared.dataTask(with: jobUrl!) { (data, response, error) in
                do
                {
                    if error == nil
                    {
                        self.jobOverViewArray = try JSONDecoder().decode([OverViewJob].self, from: data!)
                        for mainJobArr in self.jobOverViewArray
                        {
                            DispatchQueue.main.async {
                                self.jobTableView.reloadData()
                            }
                        }
    let companyDefaultValue = UserDefaults.standard
                        if let userID = companyDefaultValue.string(forKey: "user_type_id"){
                            print("Company ID or user type ID=\(userID)")
                            self.jobFilterOverviewArray = self.jobOverViewArray.filter {$0.company_id == userID}
                            print("Filter job =\(self.jobFilterOverviewArray)")
                            print("Main Array=\(self.jobOverViewArray)")
                        }
                        print("Job Data****\(self.jobOverViewArray)")
                    }
                }
                catch
                {
                    //                print("my data=\(self.mainCellData)")
                    print("Error in get JSON Data\(error)")
                }
                }.resume()
        }ray.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
            if jobTableView.isHidden == false{
                let cell = Bundle.main.loadNibNamed("VendorJobPostTableViewCell", owner: self, options: nil)?.first as! VendorJobPostTableViewCell
                let companyDefaultValue = UserDefaults.standard
                let userID = companyDefaultValue.string(forKey: "user_type_id")
                let overviewJobData = jobOverViewArray[indexPath.row]
                if userID == overviewJobData.company_id{
                    print("cell userID fetch=\(String(describing: userID))")
                    print("cell companyID fetch=\(overviewJobData.company_id)")
                    cell.LabelOne.text = overviewJobData.desig
                    cell.lblTwo.text = overviewJobData.exp_from
                    cell.lblThree.text = overviewJobData.location
                    cell.lblFour.text = overviewJobData.edu_qual
                }
                return cell
            }
        }
Audrey Rozario
  • 59
  • 1
  • 10

4 Answers4

3

This is the follow-up question of this as I believe. If you paid some attention to my answer there, you could come up with the solution. But it seems you didn't carefully try to understand the answer.

All you need is just filter method.

In your other question I applied filter as:

override func viewDidLoad() {
    super.viewDidLoad()
    // Here you apply your filter
    filteredArray = arrayOne.filter { $0.cellID == $0.cellID2 }
}

Look at the above snippet, I'm telling filter to find only those items that matches the condition:

{ $0.cellID == $0.cellID2 }

You maybe puzzled with $0 here. It's a shorthand. If you want to know what's behind the scene, let's take a look here:

override func viewDidLoad() {
    super.viewDidLoad()
    // Here you apply your filter
    filteredArray = arrayOne.filter({ (elementOfArray) -> Bool in
        return elementOfArray.cellID == elementOfArray.cellID2
    })
}

So the filter expects a closure of type of (ArrayElementType) -> Bool. So inside the closure you get the array element and you have to satisfy the return type by returning a Bool type based on your requirement.

In your other question, you wanted to compare by matching two of the properties of your struct. But here, you want to match with a plain String. So it shouldn't be very different from that I already answered. You just replace one of the properties of your struct with the plain String you have and use the actual property with which you want to match.

Something like:

override func viewDidLoad() {
    super.viewDidLoad()
    // Here you apply your filter
    filteredArray = arrayOne.filter { $0.cellID == "string from your user defaults key" }
}

Edit:

override func viewDidLoad() {
    super.viewDidLoad()
    //MARK:: getting values by userdefaults

    let companyDefaultValue = UserDefaults.standard
    if let userID = companyDefaultValue.string(forKey: "user_type_id"){
        print("User type ID=\(userID)")
        jobFilterOverviewArray = jobOverViewArray.filter{ $0.company_id == userID }
    }
}

And don't forget to use jobFilterOverviewArray as your data source. Like in the func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) method.

nayem
  • 7,285
  • 1
  • 33
  • 51
2

You need to filter jobOverViewArray before you load your table

What filter you need here check what jobOverViewArray have and use any filter condition you need to filter data before display

override func viewDidLoad() {
        super.viewDidLoad()
        //MARK:: getting values by userdefaults

        let companyDefaultValue = UserDefaults.standard
        if let userID = companyDefaultValue.string(forKey: "user_type_id"){
            print("User type ID=\(userID)")

        // **$0.id** filter with id   check what you want and write it 
            jobOverViewArray = jobOverViewArray.filter {$0.id == userID}
        }




    }
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
1

Although you got your answer, but I found some inconsistencies like reloading table by iterating over jobOverViewArray.count, check count_id in cellForRowAtIndexPath again. You can update your code as follows:

Note: I am supposing you just need to show the OverViewJob, whose c_id == user_type_id

struct OverViewJob: Decodable {
    let id: String
    let c_id: String
    let desig: String
    let exp_from: String
    let location: String
    let edu_qual: String
}

class OverviewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var jobOverViewArray = [OverViewJob]()
    var jobFilterOverviewArray = [OverViewJob]()

    @IBOutlet weak var jobTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        getJobOverviewData()
    }

    func getJobOverviewData() {
        let jobUrl = URL(string: "http://172.16.1.22/Book-My-BIM-API/Get-Job-API/get-jobs/")
        URLSession.shared.dataTask(with: jobUrl!) { (data, response, error) in
            do {
                if error == nil {
                    self.jobOverViewArray = try JSONDecoder().decode([OverViewJob].self, from: data!)
                    let companyDefaultValue = UserDefaults.standard
                    if let userID = companyDefaultValue.string(forKey: "user_type_id") {
                        print("Company ID or user type ID = \(userID)")
                        self.jobFilterOverviewArray = self.jobOverViewArray.filter{ $0.c_id == userID }
                    }

                    // Reload table view
                    DispatchQueue.main.async {
                        self.jobTableView.reloadData()
                    }
                }
            } catch {
                print("Error in get JSON Data\(error)")
            }
            }.resume()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return jobFilterOverviewArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("VendorJobPostTableViewCell", owner: self, options: nil)?.first as! VendorJobPostTableViewCell

        let overviewJobData = jobFilterOverviewArray[indexPath.row]
        cell.LabelOne.text = overviewJobData.desig
        cell.lblTwo.text = overviewJobData.exp_from
        cell.lblThree.text = overviewJobData.location
        cell.lblFour.text = overviewJobData.edu_qual

        return cell
    }
}
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
  • hello can you help me with my new question https://stackoverflow.com/questions/50501640/compare-value-from-two-struct-in-swift – Audrey Rozario May 24 '18 at 05:32
  • @AudreyRozario, let me check. Will answer there. – Ankit Jayaswal May 24 '18 at 06:09
  • @AudreyRozario, what is your desired output there? Do you want the objects from `oneData`, whose id matched with objects of array `twoData` or you want those objects from array `twoData`, whose id matched with objects of array `oneData`. – Ankit Jayaswal May 24 '18 at 06:34
  • if the id of oneData and the id of twoData matches then it should store in another array and print that new array – Audrey Rozario May 24 '18 at 06:59
  • 1
    `let mainArray = oneData.filter { i in twoData.contains { i.ID == $0.ID } }` will work for you and it will get all the objects from array `oneData` whose `ID` matched with any object's ID of array `twoData`. Just keep in mind that in this case your `mainArray` would be of type array of struct `One` as `[One]`. – Ankit Jayaswal May 24 '18 at 07:04
  • I am getting only oneData – Audrey Rozario May 24 '18 at 08:41
  • Added an answer @AudreyRozario, from which I am able to get 4 objects in `mainArray`. Please try executing the code I pasted there. – Ankit Jayaswal May 24 '18 at 08:42
  • @AudreyRozario, Please accept one of the mentioned answer for your question( This question as well). Thanks. – Ankit Jayaswal May 24 '18 at 08:58
0

Update your this line

if userID == overviewJobData.company_id{

with the following and then check.

let companyId = overviewJobData.company_id as? String
if (userID == companyid){

This will work.

aBilal17
  • 2,974
  • 2
  • 17
  • 23