问题
Below is example code of MessageKit library. Currently, its behavior is that all image messages must be loaded completely before displaying to end user so it takes a lot of time to wait. What i want is to load image after all messages have been loaded.
Look at 1/ you will see that the "kind" variable which will be a mediaItem Look at 2/ you will see that the mediaItem has "url" variable Look at 3/ you will see that the message is created with above mediaItem
The question is how to get the url of the message at 3/? I tried below but swift does not understand
let imkind = message.kind
imkind.photo.url // error Enum element 'photo' cannot be referenced as an instance member
Below are 1/ 2/ 3/
1/ Message type protocol
public protocol MessageType {
/// The sender of the message.
var sender: Sender { get }
/// The unique identifier for the message.
var messageId: String { get }
/// The date the message was sent.
var sentDate: Date { get }
/// The kind of message and its underlying kind.
var kind: MessageKind { get }
}
2/ Create an image to put into the message Kind as below:
private struct ImageMediaItem: MediaItem {
var url: URL?
var image: UIImage?
var placeholderImage: UIImage
var size: CGSize
init(image: UIImage) {
self.image = image
self.size = CGSize(width: 240, height: 240)
self.placeholderImage = UIImage()
}
}
3/ Create a message based on the MessageType above:
internal struct MockMessage: MessageType {
var messageId: String
var sender: Sender
var sentDate: Date
var kind: MessageKind
private init(kind: MessageKind, sender: Sender, messageId: String, date: Date) {
self.kind = kind
self.sender = sender
self.messageId = messageId
self.sentDate = date
}
init(image: UIImage, sender: Sender, messageId: String, date: Date) {
let mediaItem = ImageMediaItem(image: image)
self.init(kind: .photo(mediaItem), sender: sender, messageId: messageId, date: date)
}
}
4/ messageKind in the pod library
/// An enum representing the kind of message and its underlying kind.
public enum MessageKind {
/// A standard text message.
///
/// - Note: The font used for this message will be the value of the
/// `messageLabelFont` property in the `MessagesCollectionViewFlowLayout` object.
///
/// Using `MessageKind.attributedText(NSAttributedString)` doesn't require you
/// to set this property and results in higher performance.
case text(String)
/// A message with attributed text.
case attributedText(NSAttributedString)
/// A photo message.
case photo(MediaItem)
/// A video message.
case video(MediaItem)
/// A location message.
case location(LocationItem)
/// An emoji message.
case emoji(String)
/// A custom message.
/// - Note: Using this case requires that you implement the following methods and handle this case:
/// - MessagesDataSource: customCell(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell
/// - MessagesLayoutDelegate: customCellSizeCalculator(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CellSizeCalculator
case custom(Any?)
}
回答1:
guard case .photo(let mediaItem) = imkind else {
return
}
print(mediaItem.url)
You have to unwrap enum case using this code. The problem is that you tries to retrieve enum as mediaItem. But you have to retrieve the internal data.
回答2:
you can use a switch case as MessageKind is an enum
switch message.kind {
case .photo(let photoItem):
if let urlsel = photoItem.url{
print(urlsel) // here is your imageUrl
}
default:
break
}
来源:https://stackoverflow.com/questions/53787852/swift4-messagekind-has-no-member-url