Skip to content

Cannot subclass User any more #5079

Closed
getsentry/sentry-docs
#13509
@DarkDust

Description

@DarkDust

Platform

macOS

Environment

Production

Installed

CocoaPods

Version

8.49.0

Xcode Version

16.2

Did it work on previous versions?

8.41.0

Steps to Reproduce

We have a subclass of Sentry.User with additional informations. Due to the addition of the Codable support in a recent version of the Sentry SDK, this subclass cannot be used any more. The Swift compiler complains:

'required' initializer 'init(from:)' must be provided by subclass of 'User'

However, we cannot provide it, either, since then Swift complains:

initializer 'init(from:)' is declared in extension of 'User' and cannot be overridden

Code to reproduce:

class MyUser: Sentry.User {
	let moreInfo: String
	
	init(userId: String, moreInfo: String) {
		self.moreInfo = moreInfo
		
		super.init(userId: userId)
	}
	
	required init() {
		fatalError("init() has not been implemented")
	}
	
	@available(*, deprecated, message: "…")
	required convenience init(from decoder: any Decoder) throws {
		fatalError("init(from:) has not been implemented")
	}
}

Expected Result

Existing subclass of User should still compile.

Actual Result

We were able to work around this problem by using a convenience initializer, but lost the ability to provide additional informations.

As far as I can see, the only real solution would be to port the whole User class to Swift and have the codable implementation in the main class instead of an extension:

@objc(SentryUser)
class User: NSObject, SentrySerializable, NSCopying, Codable {
   …
}

This way subclasses are able to override the decoding initializer.

Are you willing to submit a PR?

maybe

Metadata

Metadata

Labels

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions