Dear Developers,
I've written a struct which conforms to Encodable. To make this work, the type of every property should also be Encodable. In this struct, named "Message", I have a custom type (which is a protocol named "TelegramId") that has already conformed to Encodable and also offered a default implementation of encode(to encoder: Encoder) through extension, however, when I am trying to build the code, the compiler said that "Message" doesn't conform to Encodable because "TelegramId" doesn't conform to Encodable, which it actually has conformed to, so I wonder how I can get rid of this error?
Code & Error Message
Code:
struct Message :
struct Message: Encodable {
let chatId: TelegramId
let text: String
let mode: String = "MarkdownV2"
let mute: Bool
enum CodingKeys: String, CodingKey {
case chatId = "chat_id"
case text
case mode = "parse_mode"
case mute = "disable_notification"
}
init(to chat: TelegramId, content: String, mute: Bool) {
self.chatId = chat
self.text = content
self.mute = mute
}
}
protocol TelegramId:
public protocol TelegramId: Encodable {
var rawId: TelegramRawId { get }
init(_: TelegramRawId)
}
extension TelegramId {
public static func id(_ id: Int) -> Self {
return Self(.id(id))
}
public static func name(_ name: String) -> Self {
return Self(.name(name))
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.rawId)
}
}
enum TelegramRawId (the type of the property "rawId" in "TelegramId"):
public enum TelegramRawId: Encodable {
case id(Int)
case name(String)
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .id(let userId):
try container.encode(userId)
case .name(let userName):
try container.encode("@" + userName)
}
}
}
error message:
Type 'Message' does not conform to protocol 'Encodable' Cannot automatically synthesize 'Encodable' because 'TelegramId' does not conform to 'Encodable' Thank you so much for your time to view my question and answer it!
Post
Replies
Boosts
Views
Activity
Hello Developers,
I just wrote the following code and tried to see the corresponding UI it generates, but it ended up that Xcode failed to compile the code and yielded some errors that I don't think could offer any info about what's wrong, so I'm here to ask if anybody could help me with my problem. Thank you so much in advance!
I tried to run this code in both a project as well as a playground file. In the playground file it kept compiling and showed nothing in the live view (it also didn't show any errors in this case) and in the project it yielded the errors and warnings shown in this post following the code.
Besides, in both cases, the code editor seemed to act weirdly as well.
Code:
import SwiftUI
struct ContentView: View {
@State var istapped = false
@State var tapCount = 0 {
mutating didSet(newValue) {
switch newValue {
case 1:
tapCountText = "once"
case 2:
tapCountText = "twice"
default:
tapCountText = "for \(newValue) times"
}
}
}
var tapCountText = String()
var body: some View {
VStack {
HStack {
Image(systemName: "star.fill")
.onTapGesture {
self.istapped.toggle()
self.tapCount += 1
}
VStack {
Text(istapped ? "A tapped star" : "A star")
.animation(.default)
Text("Just a plain star")
.font(.caption)
.foregroundColor(.gray)
}
}
Spacer()
Text("You have tapped the star " + tapCountText)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Errors and Warnings:
Error: Segmentation fault: 11 Warning:
Could not read serialized diagnostics file: Cannot Load File: Failed to open diagnostics file My Xcode version: 11.6 (11E708)
Thank you so much for your time.