Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

391
Views
'let' property may not be initialized directly; use "self.init(...)" or "self = ..." instead

I want to try to write a default init in a protocol extension, so I did this:

protocol P {
    var data: [AnyHashable: Any] { get }
    init(_ s: String)
}

extension P {
    init(_ s: String) {
        self.data = ["s": s]
    }
}

But I'm getting this error:

'let' property 'data' may not be initialized directly; use "self.init(...)" or "self = ..." instead

I have no idea what it means. Thanks for your help!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The error says that the variable cannot be initialized in the protocol extension because it's declared as constant { get }

But even if you declare the variable { get set } you can't use the init method anyway without a constraint to a concrete type or an associated type.

over 4 years ago · Santiago Trujillo Report

0

import PlaygroundSupport

protocol P {
    var data: [AnyHashable: Any] { get }
    var _data: [AnyHashable: Any] { get set }
    static func generator(_ s: String) -> P
    init()
}

extension P {
    static func generator(_ s: String) -> P {
        var p = self.init()
        p._data = ["s": s]
        return p
    }
}

class A: P {
    required init() { }
    var data: [AnyHashable : Any] {
        return _data
    }
    var _data: [AnyHashable : Any] = [:]
}

let a = A.generator("some")
print(a.data)

PlaygroundPage.current.finishExecution()

I think this is the closest code you want. The problem is that _data is shared by other classes.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!