# Initializer is inaccessible due to 'private' protection level

**URL:** <https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808>\
**Category:** Using Swift\
**Tags:** init\
**Created:** [January 23, 2022, 10:50pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808 "2022-01-23T22:50:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [January 23, 2022, 10:50pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/1 "2022-01-23T22:50:37Z")

</div>

Hi,

I am not sure why the compilation error `initializer is inaccessible due to 'private' protection level` is thrown

```swift
struct Car {
    var name: String
    private var price = 0 //changing it to let or making it internal fixes the issue
}

let car = Car(name: "aaa") //'Car' initializer is inaccessible due to 'private' protection level

```

# Question:

Wouldn't the compiler be able to detect that in all execution flows `price` would be initialised and therefore be able to synthesise the default initialiser?

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [January 24, 2022, 11:12am UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/2 "2022-01-24T11:12:40Z")

</div>

`price` is private, so the synthesized member-wise initializer for `Car` is private. Therefore, you can't call it from outside the type. I'm not sure how your comment about "execution flows" applies here.

The synthesized member-wise initializer is:

```swift
init(name: String, price: Int = 0)

```

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [January 24, 2022, 11:59am UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/3 "2022-01-24T11:59:16Z")

</div>

@Peter-Schorn Thank you so much for the clear explanation

But it seems to work when it is `private let`.

So what happens in `private let` that is different from `private var`?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [January 24, 2022, 12:01pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/4 "2022-01-24T12:01:55Z")

</div>

A `private let` with an initializer doesn't need to be a parameter in the synthesized `init`.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [January 24, 2022, 12:06pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/5 "2022-01-24T12:06:22Z")

</div>

When an instance property is declared as a `let` constant and contains an initialization expression (in your case, `0`), it does not appear in the synthesized initializer. Furthermore, it is not possible to set this property in any initializer, because initialization expressions are executed before the body of the initializer.

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [January 24, 2022, 12:12pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/6 "2022-01-24T12:12:09Z")

</div>

@Avi Thank you so much for explaining that

@Peter-Schorn I think the following makes it even more clear

> Furthermore, it is not possible to set this property in any initializer, because initialization expressions are executed before the body of the initializer.

Please bear with me as (I am a bit slow to understand) I have one bit, wouldn't `private var` also be initialised using the initialization expressions?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [January 24, 2022, 12:21pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/7 "2022-01-24T12:21:30Z")

</div>

> [@somu](#):
>
> I have one bit, wouldn't `private var` also be initialised using the initialization expressions?

The difference is that a `var` can be changed (such as by the synthesized `init`). A `let` can only be initialized once, so properties which include an initialization in their declaration can't be changed, not even by `init`.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [January 24, 2022, 12:22pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/8 "2022-01-24T12:22:54Z")

</div>

> [@somu](#):
>
> wouldn't `private var` also be initialised using the initialization expressions?

Yes, but because it is mutable, you can assign another value to it in the body of an initializer.

When an instance property does not appear in the member-wise initializer—for reasons I explained above—its access level has no effect on the access level of the initializer.

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [January 24, 2022, 12:26pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/9 "2022-01-24T12:26:05Z")

</div>

@Avi @Peter-Schorn

Thank you so much guys, the last round of explanations sealed it for me, I understand much better, thank you so much for patiently answering my repeated questions.

---

<div class="post-metadata">

**Author:** ![Georgy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/georgy/32/32187_2.png) [@Georgy](https://forums.swift.org/u/Georgy)\
**Post date:** [October 17, 2023, 5:45pm UTC](https://forums.swift.org/t/initializer-is-inaccessible-due-to-private-protection-level/54808/10 "2023-10-17T17:45:30Z")

</div>

You just need to create your own init and it will solve your problem

```swift
init(name: String) {
   self.name = name
}

```
