Swift Array Adapter

Pratik Sodha
3 min readOct 18, 2020
https://dribbble.com/shots/1074099-Array/attachments/1074099-Array?mode=media

In iOS it’s a simple thing to iterate an array and display data upon to view; It’s another thing to do something with sorting, filtering, or both together.

Let’s say we have some records and that are displayed over table view. And for the same table view have the feature to show data after sort and filter.

So, most of us doing some conditional work and manage some flags to manage our data source.

So, here ArrayDataAdapter usage concept comes into the role. This adapter class requires your array data source and that’s it. It will take care of your filtered data, sorted data, or a combination of both sorted and filtered data. So, no more conditional works.

Let’s quickly dive into a more interesting part, Yes, you are right about the coding part.

ArrayDataAdapter simple plug and surf class which able to work with all data types, custom data type (model class), or even with JSON array also. As we earlier mention adapter class takes our data source array and keeps track of our operational data.

Let’s understand using one example. Here one WebSeires model with a data loading method from the JSON file.

struct WebSeires : Codable {
let name : String
let imageName : String
let rating : Double
let genre : [String]
let numberOfSeasons : Int
let description : String
}
//MARK:- JSON file loading
extension WebSeires {
static func loadUsingJSON() -> [WebSeires] {

guard let resourceUrl = Bundle.main.url(forResource: "response", withExtension: "json") else {

return []

}
do {
let jsonData = try Data.init(contentsOf: resourceUrl)
let tempalte = try JSONDecoder().decode([WebSeires].self, from: jsonData)
return tempalte } catch {
print("Decoding Fail ==::\(error)")
}
return [] }}

Create an adapter instance with WebSeires array which is loaded from the bundle resource file.

//1
let adapter = ArrayDataAdapter(items: WebSeires.loadUsingJSON())
//2
adapter.sortComparator = { lhs, rhs in
return lhs.name < rhs.name
}
//3
adapter.filterBlock = { items in
return items.filter { (series) -> Bool in
return series.genre.contains(text)
}
}
//4
print(adapter.operationalItems)
  1. Simple ArrayDataAdapter instance creation from JSON data array.
  2. Applied sorting order for the data adapter.
  3. Applied filter to the data adapter.
  4. The immediate effect of sorting and filtering will be reflected in the adapter.operationalItems.

Sorting Comparator

As above we use an inline sorting comparator block. We can also define different sorting comparator and reuse them as per our need. Below some examples of our web series model.

extension WebSeires {

static var nameSortDescriptorAscending : (WebSeires,WebSeires) -> Bool = { lhs, rhs in
return lhs.name < rhs.name
}
static var ratingSortDescriptorDecending : (WebSeires,WebSeires) -> Bool = { lhs, rhs in return lhs.rating > rhs.rating
}
}//1
adapter.sortComparator = WebSeires.nameSortDescriptorAscending
//2
adapter.sortComparator = WebSeires.ratingSortDescriptorDecending
//3
adapter.sortComparator = nil
//4
print(adapter.operationalItems)
  1. To sort array data in name ascending order.
  2. To sort array data in rating descending order.
  3. To clear sort order, An array of data will be placed in its original position.
  4. Necessary sorting changes will be reflected in adapter.operationalItems.

Filter data block

As above we use a filter block of data adapter to filter web series data according to a genre. That can be used for any property.

//1
adapter.filterBlock = { items in
return items.filter { (series) -> Bool in
return series.rating > 9
}
}
//2
adapter.filterBlock = nil
//3
print(adapter.operationalItems)
  1. Filter block of array adapter with series has more than 9 ratings.
  2. To clear down filtering over data.
  3. Respective changes will be reflected in adapter.operationalItems.

Filtering block also work as searching functionality.

--

--

Pratik Sodha

Software developer at Spaceo Technologies. Loves to create iOS apps. Github : https://github.com/Pratik-Sodha