0

I have created my own Struct object, that is populated from the Photos framework with each "moment" an image was contained in, and other metadata.

struct MomentImage {
    var momentNo : Int
    var index : Int
    var image : UIImage
}

I would like to populate a tableView, split with:

  • Sections, which map to momentNo
  • Cells, which map to index

In tableView(cellForRowAt indexPath), I have printed out:

print("location: \(indexPath.section) and \(indexPath.row)")

But the Section value only ever prints "0". I expected my print statements to occur through all the sections in this table (with my test data, this is actually 13 sections)

How do I filter / retrieve this object at these 2 indices from my array of var moments = [MomentImage]...

Using array.filter { } seems the most obvious way. How do I pass in a 2nd property to check (i.e. momentNo AND index) within the same call?

let filteredassets = assets.filter { $0.momentNo == indexPath.section }

is my current code.

Note, the images need to be encodable, hence extracting from the PHImageManager into a distinct UIImage within a Struct that should itself be encodable.

2
  • You can try let filteredassets = assets.filter { $0.momentNo == indexPath.section && ... } ? Commented Jul 23, 2019 at 10:41
  • @Vicaren you were correct, hadn't even thought of this construct Commented Jul 24, 2019 at 9:09

1 Answer 1

1

As provided by @Vicaren, applying a && after the 1st parameter converts into to a 2-property IF AND statement

$0 is still available to select, simply choose another available property to test.

My own example results in:

let filteredassets = assets.filter { $0.momentNo == indexPath.section && $0.index == indexPath.row }

Sign up to request clarification or add additional context in comments.

Comments

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.