6

I have an array of Business objects. Each Business object contains an array of key-value pairs, one element of which can be a nested array of ContentBlocks objects.

var masterArray = [
        Business(busName: "Dave's Cafe", busId: 1, website: "http://www.davescafe.com", latLong: (45.541, -45.609),
            actions: [["title": "About Us", "contentId": "123", "actionType": "content"],
                ["title": "Website", "url": "http://www.davescafe.com", "actionType": "web"]],
            contentBlocks:[
                ContentBlock(busName: "Dave's Cafe", busId: 1, contentId: "123", title: "Testola!", body: "Hello there!")
            ]),
        Business(busName:...
]

I can filter the array to return a specific Businesses matching a unique busId by using something like this:

let rtnArray = masterArray.filter{$0.busId == id}
    if rtnArray.count == 1{
        return rtnArray[0]
    } else {
        return // feedback that no matches were found
    }

Additionally, I'd like to return a specific contentBlock by filtering on the unique contentId (if necessary I can also pass the busId of the Business 'owner'). I'm really struggling to move forward so any pointers in the right direction would be great.

3 Answers 3

9

Here's a solution to what I think you're asking:

 var contentBlocks = masterArray
                         .flatMap{$0.contentBlocks}
                         .flatMap{$0}
                         .filter{$0.contentId == "123"}

Outputs a [ContentBlock] containing all ContentBlock objects that match the filter from within all Business objects.

  1. The first flatMap makes the list of Businesses into an [ContentBlock?]
  2. The flatMap flattens the [ContentBlock?] into a [ContentBlock]
  3. The [ContentBlock] is filtered
Sign up to request clarification or add additional context in comments.

14 Comments

thanks for a great answer. When I try it the compiler complains: Value of type '[ContentBlock]' has no member 'contentId' on the last line. Any idea what I'm doing wrong?
Well if contentID is a key it would be written like $0["contentId"] to access the value of that key. Compiler is giving you error because it is currently being accessed like a property which I assume it is not.
@NSGangster Thanks - I tried that but it complains again, this time with Cannot subscript a value of type '[ContentBlock]' with an index of type 'String' So I tried without quotes - same error...Hmmm!
Mocking this up would take me a while (i'd need to make those structs and what not). Could you tell me the type you get after map, and then the type you get after flatMap?
|
3

When you are dealing with model and want to search the nested custom object of class.

Here is class example.

public final class Model1  {
// MARK: Properties

public var firstname: String?
public var lastname: String?
public var email: String?
public var availabilities: [Availability]?
public var username: String?
}

public final class Availability {
var date: String
var day : String

init(date: String, day: String) {
    self.date = date
    self.day = day

    }
}
public final class AvailData {
var am: String?
var pm: String?

init(am: String?,pm: String?) {
    self.am = am
    self.pm = pm
   }
}

let make an array of Model1.

var arrData = [Model1]()

In my case, I want to search in availabilities array. So I put below logic and it work perfectly.

 let searchData = arrData.filter({ singleObj -> Bool in
     let result = singleObj.availabilities?.filter({ $0.date == "2019/09/17" })
     return result?.count > 0 ? true : false
 })

This swift code will return only those records which match 2019/09/17

Comments

2

Try this:

// The busId and contentId you want to find
let busId = 1
let contentId = "123"

let contentBlocks = masterArray.flatMap {
    $0.contentBlocks.filter { $0.busId == busId && $0.contentId == contentId }
}

1 Comment

Thanks for this. The answer stackoverflow.com/a/37730229/131385 is what I was thinking might be possible (only requires the contentId). Your suggestion will be perfect for a similar but slightly different data structure + filter I need elsewhere in my app.

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.