Repository navigation
SwiftData class: Method does not override any method from its superclass #56
Description
Activity
If a SwiftData model annotation is added to a class, the build will fail with "Method does not override any method from its superclass".
It can be reproduced using a clean SwiftData project, adding the SPM package from 1.2.1, and the following class:
import Foundation import SwiftData import MetaCodable import HelperCoders protocol MyProtocol { } @Codable @Model final class Item : MyProtocol { @CodedAt("timestamp") var timestamp: Date? = nil init() { } }
Are you sure you are able to reproduce this with this code snippet, for me this code snippet doesn't produce error. Can you provide a sample project for this?
I have however managed to produce one expansion with the error:
Can you provide sample project for this, I can think of one scenario where this error would come:
Note
If you are manually conforming to
Codableprotocol in the class, please remove the conformance as@Codablemacro itself generates the conformance.Anyway I can't do much here with the screenshots, please provide a minimal reproducible sample.
- addedquestionFurther information is requestedFurther information is requested
on Jan 17, 2024 Here's a repo: https://github.com/ashleydw/metacodable-min-ex/blob/main/test/Item.swift
If you build you should get the same error on Item.swift. Commenting out
@CodableandCodedAtshould build successfully and run.Reacted by Soumya Ranjan MahuntThanks @ashleydw for providing sample, I can reproduce your issue. The issue doesn't seem to be combining
@Codablewith SwiftData class, in fact the project builds successfully if you comment out following lines:https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L6
https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L11-L18
https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L50
https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L58Seems like with these line present Swift is not providing protocol conformance data to
@Codable. It would be better if you create a discussion on Swift forum or Apple developer forum as the issue doesn't seem to beMetaCodablerelated.Reacted by Ashley WhiteSame issue here,
problem magically disappear when
#Previewsection commented out of some random View (no@ State, no@Queryno anything), other#Previewthat contains a@Querynot affected by thisAnd it happens to me when I move
@Bindable var someEnvObject = someEnvObjectin scope of a body of a View that takes and EnvroementObject (check below for doc), to 1ZStacklevel inside...// @Bindable var someEnvObject = someEnvObject <- from here ZStack { @Bindable var someEnvObject = someEnvObject <- to here // some other code using `someEnvObject` }
(Apple doc)[https://developer.apple.com/documentation/swiftui/bindable]
@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?
I am afraid this issue is not related to
MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?
I am afraid this issue is not related to
MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.I haven't, I wasn't able to identify where the problem lies to.
The only thing I can be sure about is that it happens with this use case and reporting this exact error from@Codablegenerated code.@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?
I am afraid this issue is not related toMetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.I haven't, I wasn't able to identify where the problem lies to. The only thing I can be sure about is that it happens with this use case and reporting this exact error from
@Codablegenerated code.Simple overview of the class implementation generation by
@Codable:-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`) -> Yes -> Are `Decodable`/`Encodable` conformance implemented i.e. `init(from:)`/`encode(to:)` method implementation exist? -> Yes -> Skip generating implementation -> No -> Assume that super class conforms to `Decodable`/`Encodable` and provide generated implementation. -> No -> No super class conforms to `Decodable`/`Encodable` and provide generated implementation.The strange thing here is without those lines the Swift compiler invokes the following flow:
-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`) -> No -> No super class conforms to `Decodable`/`Encodable` and provide generated implementation.and with those lines present the Swift compiler invokes the following flow:
-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`) -> Yes -> Are `Decodable`/`Encodable` conformance implemented i.e. `init(from:)`/`encode(to:)` method implementation exist? -> No -> Assume that super class conforms to `Decodable`/`Encodable` and provide generated implementation.which is a strange behaviour from compiler, as of why compiler provides wrong data in some cases, only Swift compiler team can answer.
@soumyamahunt great insight!
@soumyamahunt @ashleydw Are you available to fire a bug report or discussion as you said to apple by any chance?
I don't think I'm qualified enough for this as I'm new to SwiftLang let alone any insight on how swift macro works.If not, do you think there is way to go around with this problem? I am very interested in this cross usage on SwiftData+MetaCodable, as it would vastly simplify my code in a project that I started recently. (Actually I'm current on a track giving up on
#Previewcapability just to have this working)Reacted by Andrew- pinned this issue
on Mar 27, 2024 @ashleydw @Willian-Zhang new macro
Inheritshas been added (only inmainbranch, not released yet) to workaround the issue until this is fixed from compiler.Reacted by Willian Z and Radu UrsacheReacted by Willian Z and Ashley WhiteReacted by Willian Zhaving Inherits tested, I can confirm this solved my problem perfectly.
Reacted by Soumya Ranjan Mahuntnot sure what i'm doing wrong. using the
mainbranch and this code:@Codable @Model @Inherits(decodable: false, encodable: false) final class TestModel: Codable, Identifiable { var id: Int = 0 var url: String = "" var name: String = "" init(id: Int, url: String, name: String) { self.id = id self.url = url self.name = name } }
Fails with
Redundant conformance of 'EpisodeModel' to protocol 'Decodable'
Redundant conformance of 'EpisodeModel' to protocol 'Encodable'EDIT: solved by removing the
Codable, Identifiableconformance, my bad!@rursache you have to remove the manual
Codableconformance you are adding, due to this addition compiler is also generatingCodableimplementation which is conflicting with this library's generated implementation.Reacted by Radu Ursache@soumyamahunt I can confirm it works in my project, thanks!
Reacted by Soumya Ranjan MahuntPlease, release a new version with this fix
@bfalcinelli currently the proper fix can be done from compiler end, as the issue arises due to compiler providing wrong data to 'MetaCodable'.
The current work around has been released: https://github.com/SwiftyLab/MetaCodable/releases/tag/v1.4.0

If a SwiftData model annotation is added to a class, the build will fail with "Method does not override any method from its superclass".
It can be reproduced using a clean SwiftData project, adding the SPM package from 1.2.1, and the following class:
Expanding the macro actually shows it doesn't add the
super.*calls:I have however managed to produce one expansion with the error: