2

I've two structs, a User and Chat

struct User {
    let appId: String
}

struct Chat {
    var users: [User]
}

So if you can see a chat can have multiple users.

let u1 = User(appId: "12345")
let u2 = User(appId: "6789")
let chat = Chat(users: [u1, u2])

Now I'd like to detect if these two users are inside the chat, maybe by the appId

I tried

let contains = chat.users.contains { inUser -> Bool in
    return inUser.appId == "12345" && inUser.appId == "6789"
} 

But for sure that will not work.

Someone can help me? Thanks!

1 Answer 1

1

Since you want to know if u1 and u2 are into the chat you should write

let bothInChat = chat.users.contains { $0.appId == "12345" } && chat.users.contains { $0.appId == "6789" }
Sign up to request clarification or add additional context in comments.

1 Comment

Dooooh, that easy one ... thank's a lot. I thought too complicated.

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.