Почему не могу создать объект структуры? Базовый вопрос

class
swift
oop

#1

Всем привет.

Пришел с с/с++, не могу понять логику почему не могу создать drJones и почему мне обязательно нужно делать init, ведь private переменная currentPatient имеет значение. Почему мне обязательно нужно делать инит? Не понимаю.

Заранее благодарю за ответ.

struct Doctor {
  var name: String
  var location: String
  private var currentPatient = "No one"
}

let drJones = Doctor(name: "Esther Jones", location: "Bristol")

#2

https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties can’t be left in an indeterminate state.


#3

Потому что проперти name и location у вас должны быть проинициализированы, что бы создать экземпляр без инициализации можно сделать их опциональными var name: String?


#4

как сообщает ошибка, всё дело в private - " ‘Doctor’ initializer is inaccessible due to ‘private’ protection level".

Вот здесь как раз поясняется:

Default Memberwise Initializers for Structure Types

The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Likewise, if any of the structure’s stored properties are file private, the initializer is file private. Otherwise, the initializer has an access level of internal.

As with the default initializer above, if you want a public structure type to be initializable with a memberwise initializer when used in another module, you must provide a public memberwise initializer yourself as part of the type’s definition

https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html