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

223
Views
In Swift if I make something with 'let' than I can't change property but why keyPath is different?
class Person{
    var name: String
    init(_ name: String){
        self.name = name
    }
}

struct Stuff{
    var name: String
    var owner: Person1
}

let jenna = Person("jenna")

var mac = Stuff(name: "Macbook pro", owner: jenna)

let ownerKeyPath = \Stuff.owner.name

mac[keyPath: ownerKeyPath] = "jerry"     

I made jenna with 'let' keyword but if I use keyPath than I could change jenna's property. How does it works?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

KeyPaths are irrelevant.

You made Person.name variable. As such, mac.owner.name = "jerry" works fine.

But it won't if you change name to let. That will make ownerKeyPath a KeyPath, not the ReferenceWritableKeyPath subclass of KeyPath that it is in your code. As with let constants, you can't set values using the KeyPath class.

over 4 years ago · Santiago Trujillo Report

0

Person is a reference type (a class). For a reference type, let just means that the reference itself cannot change, so jenna cannot be made to point to another object. But you can change any var properties within it (such as name. KeyPaths aren't related here. The same is true of regular property access:

mac.owner.name = "jerry"

If you want Person to behave like a value, so that its properties cannot change when it is marked with let (but can when it is marked with var), then it needs to be a struct, not a class. Then it will behave like Stuff.

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!