Skip to content

SwiftData class: Method does not override any method from its superclass #56

Description

@ashleydw

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() { }
}

Expanding the macro actually shows it doesn't add the super.* calls:

image

I have however managed to produce one expansion with the error:

image

Activity

  1. soumyamahunt commented on Jan 17, 2024

    @soumyamahunt
    Contributor

    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:

    image

    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 Codable protocol in the class, please remove the conformance as @Codable macro itself generates the conformance.

    Anyway I can't do much here with the screenshots, please provide a minimal reproducible sample.

  2. ashleydw commented on Jan 17, 2024

    @ashleydw
    Author

    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 @Codable and CodedAt should build successfully and run.

  3. soumyamahunt commented on Jan 18, 2024

    @soumyamahunt
    Contributor

    Thanks @ashleydw for providing sample, I can reproduce your issue. The issue doesn't seem to be combining @Codable with 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#L58

    Seems 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 be MetaCodable related.

  4. Willian-Zhang commented on Jan 21, 2024

    @Willian-Zhang

    Same issue here,

    problem magically disappear when #Preview section commented out of some random View (no @ State, no @Query no anything), other #Preview that contains a @Query not affected by this

  5. Willian-Zhang commented on Jan 22, 2024

    @Willian-Zhang

    And it happens to me when I move @Bindable var someEnvObject = someEnvObject in scope of a body of a View that takes and EnvroementObject (check below for doc), to 1 ZStack level inside...

    // @Bindable var someEnvObject = someEnvObject <- from here
    ZStack {
      @Bindable var someEnvObject = someEnvObject <- to here
      // some other code using `someEnvObject`
    }
    image

    (Apple doc)[https://developer.apple.com/documentation/swiftui/bindable]

  6. soumyamahunt commented on Jan 22, 2024

    @soumyamahunt
    Contributor

    @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.

  7. Willian-Zhang commented on Jan 22, 2024

    @Willian-Zhang

    @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 @Codable generated code.

  8. soumyamahunt commented on Jan 23, 2024

    @soumyamahunt
    Contributor

    @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 @Codable generated 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.

  9. Willian-Zhang commented on Jan 23, 2024

    @Willian-Zhang

    @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 #Preview capability just to have this working)

  10. pinned this issue on Mar 27, 2024
  11. soumyamahunt commented on Mar 27, 2024

    @soumyamahunt
    Contributor

    @ashleydw @Willian-Zhang new macro Inherits has been added (only in main branch, not released yet) to workaround the issue until this is fixed from compiler.

  12. Willian-Zhang commented on Apr 9, 2024

    @Willian-Zhang

    having Inherits tested, I can confirm this solved my problem perfectly.

  13. rursache commented on Apr 10, 2024

    @rursache

    not sure what i'm doing wrong. using the main branch 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, Identifiable conformance, my bad!

  14. soumyamahunt commented on Apr 11, 2024

    @soumyamahunt
    Contributor

    @rursache you have to remove the manual Codable conformance you are adding, due to this addition compiler is also generating Codable implementation which is conflicting with this library's generated implementation.

  15. ashleydw commented on Apr 15, 2024

    @ashleydw
    Author

    @soumyamahunt I can confirm it works in my project, thanks!

  16. bfalcinelli-tg commented on Sep 5, 2024

    @bfalcinelli-tg

    Please, release a new version with this fix

  17. soumyamahunt commented on Sep 30, 2024

    @soumyamahunt
    Contributor

    @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions