1

I have 2 structs which takes, in a list of Courses.

struct Course : Decodable {
let CourseName : String;
let EntryRequirements : String;
}

struct  WebsiteDescription : Decodable {
let FullProgramName :String
let ProgramName : String;
let Courses : [Course]
}

These structs are loaded with JSON

I've implemented a UISearchController to actually Search through the Course Name.

I have no idea how to reference to the Struct Course from within the WebsiteDescription which contains the course names.

Arrays that I had used to contain the WebsiteDescription

  var allCourses = [WebsiteDescription]()
  var filteredCourses = [WebsiteDescription]()

I have tried to filter it with this method

func updateSearchResults(for searchController: UISearchController) {
    if searchController.searchBar.text! == "" {
        filteredCourses = allCourses
        acTableView.reloadData()
    } else {
        filteredCourses = allCourses.filter { $0.ProgramName.lowercased().contains(searchController.searchBar.text!.lowercased()) }



    }  
    self.acTableView.reloadData();
}

It filters the general programme name, but I would actually want to filter using the CourseName in the struct Course.

Thank you in advance.

This is what I currently get

When I search for mentoring, the whole section of ShortCourses Appears when only 'Mentoring' should appear.

Mentoring is the Course Name

2
  • is allCourses array of WebsiteDescription ? Commented Dec 21, 2017 at 5:47
  • yes, it is an array of WebsiteDescription Commented Dec 21, 2017 at 5:48

1 Answer 1

1

Replace this line

allCourses.filter { $0.ProgramName.lowercased().contains(searchController.searchBar.text!.lowercased())

with this

allCourses.filter {
             !$0.Courses.filter{$0.CourseName.localizedCaseInsensitiveContains(searchController.searchBar.text!)}.isEmpty
        }

It will give the result according to your CourseName in the struct Course.

EDIT : It will give you Array of Course

allCourses.flatMap{$0.Courses.filter{$0.CourseName.localizedCaseInsensitiveContains(searchController.searchBar.text!)}}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi! It works now but it returns the entire section, how do I only show the Course Name?
Okay. Do you want [Course] or [CourseName] as a result?
Course Name, please
There was an error encountered 'flatMap' produces '[String]', not the expected contextual result type '[WebsiteDescription]'
Exactly. This is an array for string , which is course name , as per you asked. not the array of WebsiteDescription. Can you show me pictorial representation, what you really want?
|

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.