You can get an instance of a Swift struct S
in two ways:
-
You can explicitly create a new one, by calling S.init()
(or just S()
for short) with the relevant, if anyt arguments.
let s1 = S()
-
You can copy another struct instance, by assignment, or passing as an arugment into a function. Doing so does not cause your initializer to be called. It does implicitly call some compiler-generated code, akin to a copy constructor in C. This copy construction's job is to copy all members over, including calling retain
on all ARC managed objects that are members of the struct.
let s1 = S() // "Initialization" happens here
let s2 = s1 // A copy is made of s1, without initialization
An instance of a struct is an object created by using a pre-defined struct.
"instance" and "object" are synonymous, as far as I'm aware. However sometimes people use "instance", because most people usually associated objects with classes (and their associated reference semantics), from other programming lanugages
If you want to create an object using your struct as its blueprint, you must first “initialize” the struct.
I don't like using "struct" as a stand-in for "object", "instance", or "value", even though it technically makes sense when you expand the contraction ("I'll create a new structure over here..."). Doing so makes it ambiguous when you're talking about values (instances/objects) versus types, which makes conversations more confusing. But yes, if you want to create an object using your struct
("as its blueprint" is redundant, there's no other way to use a struct to make an object), you must call initializer of the struct
type.
To “initialize a struct" is to create an actual object based on the struct (a blueprint) that you can then use.
Indeed. And in Swift, implicit in initialization is the act of allocation. Allocation sets aside some memory to be used for a new object. After allocation, the contents of that memory are still whatever happened to be residing before you took ownership of it. Technically, initialization is the process of initializing that memory so that all of its memory contains known values, not indeterminate junk from whoever used it last. "Initialization" in Swift refers to doing both, and calling your init
to decide precisely what values to set the memory to.
initialize a struct explicitly by using init()
in the struct definition
which will “pre-iniitialize” it, or
"Pre-initialize" isn't a thing
simply initialize a struct by creating an instance of the struct since structs are per-initialized in Swift
I don't know what this is asking.