Skip to content

Swift - SRKObject field values not settable in any init methods #131

@MeteC

Description

@MeteC

Porting an Obj-C SRKObject implementation over to Swift today, I found my tests started failing. It turned out I couldn't use any custom init methods (nor convenience init) to provide any values for my @objc dynamic vars. Those values are simply overwritten with 0. (Something that wasn't happening in Objective-C.)

e.g. something like @objc public convenience init(withID: NSNumber, dataType: Int) fails to initialise either ID or dataType at all - within the init function the instance variables are set correctly, but upon leaving the init function they seem to be reset to 0.

Through this issue I discovered that I need to use override class func defaultValuesForEntity() -> [String : Any]? {} to provide default values on init.. So that's useful. But now I really need to put up post-it notes (literally or figuratively) to remind myself in future that I can't use custom init methods with SRKObjects in Swift.

So there's no loss of functionality ultimately, but this could definitely be a potential source of strange bugs if one's unit test coverage isn't pedantic ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions