1

I hope i'm not out of order on my fisrt post here I'm a triple-oh nooob wanting to get fundamental concepts right

I get what classes are all about, but not so clear on difference between instances and inits ( initialisations )

Can i think of it this way?:

A small building development site has a design approved. (Blueprint) That'll be the class, sure enough

The 'instance' would be one unit bought in from the factory on a truck and craned in

but would the init be when the surveyor comes and pegs out its space 'allocation' on the site ? ( I started in Objective-C before Swift, and have only just looked into swift, you can tell ) or is it something else?

because Swift documentation tels me a 'var' or a 'let' object needs a value assigned, it cant be a nil. that's strange when I see lots of material with things like

var X : Int
let str : String

I see no values there, they say they explicitly want to see X = 5 or str ="cotton"

so tat leaves two possible analogies for init() :

var X : Int // like the building has the bounds set out, it's just not filling the space?
Var X = 5  // like the building is in place, but still empty and not in habitation

not asking for any great discussion, which is the 'more right'? if any

2 Answers 2

2

Quite literally, "instantiation" or "initialization" (same thing) is when you CREATE the instance of a class. So using a similar metaphor, the Class is the blueprint for the house, the Initialization (init) is when the construction crew builds the house, and the Instance is the finished house that you live in.

In your example above, that would be var x = 5. The construction crew just built x and set it to 5. Now you can do whatever you want with your instance of x, like change it to 6. Your first example of telling everyone that x is going to be an Int is what the Class is for.

Sign up to request clarification or add additional context in comments.

Comments

1

The class is the blue print for your object. E.G. MyObject contains these variables , x, y and z and has these methods foo1, foo2, foo3. The instance of a class is a instantiation (creation) of a single object that contains values for x, y and z and can use these methods foo1, foo2, foo3.

The init of a class does any initialization the class needs, usually assigning values to the constructed objects variables, x = 3, y = 4, and z = 5. Anything that you want done upon construction of the object should be done in the init.

In swift, if you create a variable but don't initialize it with a value, e.g. var x : int, and try to use that variable you will get a compiler error. The object is instantiated but not initialized. If you need your variable to contain nil, you must use the optional marker (?), var x: int? = nil. Variable x is now of type optional.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.