1

I would like to show loaded image in the cell. I tried to make it in such way:

messages.filter { (mType) -> Bool in
                        mType.messageId == "\(message.id)"
                    }.first?.kind = .photo(ImageMediaItem(image: UIImage(systemName: "photo")!.withTintColor(UIColor.gray, renderingMode: .automatic)))

where:

var messages = [MessageType]()

but I saw a message which said:

Cannot assign to property: 'kind' is a get-only property

So I decided to add setter/getter to this property:

struct Message: MessageType {
    var sender: SenderType
    var messageId: String = ""
    var sentDate: Date
    var kind: MessageKind{
        get {
            return self.kind
        }
        set (newValue) {
            self.kind = newValue
        }
    }
}

I used such scope for adding data to chat list:

messages.append(Message(sender: message.fromId == selectedContactID ? otherSender! : currentUser!, messageId: "\(message.id ?? -1)", sentDate: Date().addingTimeInterval(-8600000), kind: mKind))

but after changes mentioned above I saw such message:

Extra argument 'kind' in call

I tried to look through documentation but I didn't manage to find smth useful for my situation. Maybe someone knows how to solve this problem?

Andrew
  • 1,947
  • 2
  • 23
  • 61

1 Answers1

1

You need to get index of the object you need to change with a struct

if let index = messages.firstIndex(where:{ $0.messageId == "\(message.id)" }) {
   messages[index].kind = .photo(ImageMediaItem(image: UIImage(systemName: "photo")!.withTintColor(UIColor.gray, renderingMode: .automatic)))
}

and for Extra argument 'kind' in call a computed property shouldn't be in init in addition to that in your case you don't need to make it computed property as you implement what's already exist by default

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • So, I can't update certain cell which will have already installed kind inside my collectionview ? – Andrew Oct 13 '20 at 10:54
  • and your code scope gives me an error `Cannot convert value of type 'MessageType' to expected argument type 'Int'` – Andrew Oct 13 '20 at 10:57
  • add the index of the object instead of the object itself as a property inside the cell BTW you can delegate everything to the vc to access the array also – Shehata Gamal Oct 13 '20 at 10:57
  • I still can't understand something , I have to change my struct of Message? because after inserting your answer I received message about get only property :( – Andrew Oct 13 '20 at 10:59
  • change it to `var kind: MessageKind` or `var kind: MessageKind?` if you need to defer init – Shehata Gamal Oct 13 '20 at 11:04
  • `Type 'Message' does not conform to protocol 'MessageType'` after adding question mark, maybe you can post more info, because I'm a newbie in ios development :) – Andrew Oct 13 '20 at 11:06