3

I need to filter Schedules[ ] Array using Dates[ ] Array where "date_id" == "schedule_date_id".

I need only those elements where date_id matches schedule_date_id and then reload the table. I need to show Schedules according to dates.

What is the Swift code for this?

Below is the JSON I have:

{
dates =     (
            {
        "date_date" = "2017-11-04";
        "date_id" = 4;
        "date_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedule-days/day_1.png";
        "date_title" = "Day 1";
    },
            {
        "date_date" = "2017-11-05";
        "date_id" = 5;
        "date_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedule-days/day_2.png";
        "date_title" = "Day 2";
    }
);
schedules =     (
            {
        "schedule_date" = "2017-11-04";
        "schedule_date_id" = 4;
        "schedule_description" = "Schedule 7";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 7;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    },
            {
        "schedule_date" = "2017-11-04";
        "schedule_date_id" = 4;
        "schedule_description" = "Schedule 8";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 8;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    },
            {
        "schedule_date" = "2017-11-04";
        "schedule_date_id" = 4;
        "schedule_description" = "Schedule 9";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 9;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    },
            {
        "schedule_date" = "2017-11-05";
        "schedule_date_id" = 5;
        "schedule_description" = "Schedule 10";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 10;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_2.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    }
);
}
4
  • It's not at all clear what you're looking to do. Are you trying to do a join? Commented Oct 17, 2017 at 5:58
  • @DavidBerry I need elements from Schedule [ ] where "schedule_id" from Schedule [ ] should match "date_id" in Dates[ ] Commented Oct 17, 2017 at 5:59
  • I think you want to match date_id with schedule_date_id not schedule_id, right ? Commented Oct 17, 2017 at 9:08
  • @RobertD.Mogos yeah, you are right Commented Oct 18, 2017 at 5:43

3 Answers 3

3

Use like this:

Swift 4

let filteredSchedules = schedules.filter { schedule in
    dates.contains(where: { $0["date_id"] as! Int == schedule["schedule_id"] as! Int }
)}
print(filteredSchedules)

Swift 3

let filteredSchedules = schedules.filter { schedule in
    dates.contains({ $0["date_id"] as! Int == schedule["schedule_id"] as! Int })
}
print(filteredSchedules)
Sign up to request clarification or add additional context in comments.

8 Comments

showing this error : Extraneous argument label 'where:' in call
Updated the answer. Please check
showing error at $0 : Cannot subscript a value of incorrect or ambiguous type
Is your schedules or dates array optional?
DateArray = EventSchedule["dates"] as! NSArray I've declared Dates array as this
|
1

To filter schedules array using date_id from datesArray

let dateId = 4
let filteredSchedules = schedules.filter { schedule in
    dates.contains(where: { dateId as! Int == schedule["schedule_id"] as! Int }
)}
print(filteredSchedules)

To assign dates to collectionView cell

cell.titleLabel.text = datesArray[indexPath.row]["datesArray"]

Comments

0

Try with below code

let newArray  = schedulesArray.filter { (schedulesDic) -> Bool in
            return datesArray.contains(where: { (datesDic) -> Bool in
                if schedulesDic["schedule_id"] as! Int == datesDic["date_id"] as! Int{
                    return true
                }
                else {
                    return false
                }
            })
        }
print(newArray)

Or short form of above code

let newArray  = schedulesArray.filter { (schedulesDic) -> Bool in
            datesArray.contains(where: { $0["date_id"] as! Int == schedulesDic["schedule_id"] as! Int })
        }
print(newArray)

1 Comment

showing error at $0 : Cannot subscript a value of incorrect or ambiguous type

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.