How swift creates instance with init()? [duplicate] - swift

This question already has an answer here:
Did not understand process of initialize in swift programming
(1 answer)
Closed 6 months ago.
This is question for curiosity, How does init() function create instance?
Why when we assign values in init method makes instance? and How?
I feel like init() is just a method with assigning values without any return types but how!!! does it make return instance when we call it?
Anyone knows the answer? I read init() document but still not getting it. Anyone knows the answer?
Thank you for reading!
struct Rect {
var origin = Point()
var size = Size()
init() {}
}
let basicRect = Rect()
How is this working????????

The initializer is used in stored property:
Initializer is used in stored property to create an initial value.
Initializer is used in stored property to assign default property value within the property definition.
In Swift, init() is used to an initialize an instance.

Related

Initializing struct value with function [duplicate]

This question already has an answer here:
Swift- error: Variable 'self.___' used before being initialized
(1 answer)
Closed 3 years ago.
I have a Swift struct which has a title property. In my initializer, I can set the property as normal. If, however, I want to extract the logic for its value to another function, I get a compiler warning.
This works:
init(document: MyDocument) {
documentIdentifier = document.documentIdentifier
createDate = document.createDate
title = "A Title"
}
This fails:
title = generateTitle(forDocument: document)
The compiler says Variable 'self.title' used before being initialized. If I put a static setter just above this line, the error goes away. My generate function returns a non-optional value. What's the compiler grumpy about?
generateTitle is an instance method, but the instance itself isn't yet created before the initialiser returns and hence you cannot call any methods on it. If you really want to extract the setup code for title into a separate method and you aren't actually accessing any instance properties from that method, you can make it static and hence accessible even from the init.

Why does this Swift struct require "mutating"? [duplicate]

This question already has answers here:
Mutating function inside class
(4 answers)
Closed 3 years ago.
I am missing something about the mutability concept in Swift. I normally use objects, not structs, to get observability, so the value semantics are still new to me.
struct Game {
var map: [[Int]]
So here I have declared map as mutable. So why in a method like this...
mutating func createPlayer() {
// emptyLocation -> (Int, Int)
let (X,Y) = emptyLocation()
map[X][Y] = .player
}
...do I have to use the mutating? Yes, the function is mutating, but the original struct is declared as such. It seems that practically every func will be mutatingin practice, which seems to defeat the purpose of the markup.
Is there some other way I should be doing this? Is the common use of mutating indicating a performance/memory issue I should be avoiding?
Update: I was rather upset at the way the internal state of the struct "leaked out" to the surrounding code; if you declare a member var inside the struct then it has to be outside as well, even if you never changed. This violates every concept of encapsulation I can think of. So I changed struct to class, removed all the mutating, and was done with it. I get the idea, but I'm not sure I fully understand the implementation. It seems, to this Swift-noob, that the mutating is something the compiler can determine without me telling it - is the member declared var?, does the func actually mutate it? etc.
Yes it is the default behaviour for a struct or enum that instance methods can not modify a property since they are value type. So you need to use mutating to override this behaviour.
The way you define your property, var or let, is still relevant for if you can change it from a mutable instance method or directly or not.
Since your property is not private you can still do
var g = Game(map: [[1]])
g.map.append([2])
In Swift structs have value semantics, ie they behave as if they are values even though some of them like String and Array are implemented as references for performance reasons. When you mutate such a struct the compiler may actually have to make a copy if you are not the only owner of the struct; this is known as copy on write and it is the possible performance issue that mutating indicates.

Stopping reference variables changing the value of the original variable

I am assigning the value of a custom class to another variable. Updating the value of the new variable is affecting the value of the original variable. However, I need to stop the reference variable from updating the original variable.
Here's a basic representation of what's happening:
var originalVariable = CustomClass()
originalVariable.myProperty = originalValue
var referenceVariable = originalVariable
referenceVariable.myProperty = updatedValue
print("\(originalVariable.myProperty)") //this prints the ->updatedValue<- and not the ->originalValue<-
I've tried wrapping the referenceVariable in a struct to make it a value type but it hasn't solved the problem.
I've found information regarding value and reference types but I haven't been able to find a solution.
My question in a nutshell: How do I stop an update to a reference variable from updating the original variable that it got its value assigned from?
Thanks in advance.
The whole point of reference semantics (as used by classes) is that all variables point to the same (i.e., they reference the same) object in memory. If you don't want that behaviour, you should use value types (Struct, Enum, Array...) or create copies of your object.
If CustomClass implements the NSCopying protocol you can do:
var referenceVariable = originalVariable.copy()
If it doesn't, you'll have to find some other way to copy it or implement the protocol yourself.
Wrapping the class in a struct will just make two different structs each containing a different reference to the same object.

What is difference between URL.init(string: "") and URL(string: "")? [duplicate]

This question already has answers here:
In Swift, what's the difference between calling UINavigationController() vs UINavigationController.init()?
(3 answers)
Closed 6 years ago.
What is difference between
URL.init(string: "")
and
URL(string: "")
Which is the better way?
The first way you stated uses an Initializer.
Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters.
This means that they are used for:
To create an initial value.
To assign default property value within the property definition.
To initialize an instance for a particular data type 'init()' is used. No arguments are passed inside the init() function.
Therefore if you just want to use the data in the same instance you should use the first second method.
If you want to have the URL in anther instance and set it as a default value for your string you should use the first one.
But ultimately they will both do the same thing.

Cannot invoke initializer for type AnyObject? with no arguments [duplicate]

This question already has an answer here:
Cannot invoke initializer for type 'AnyObject?' with no arguments
What happened to the UIView?() constructor in Swift 3.0?
(1 answer)
Closed 6 years ago.
I created a variable in my Xcode project.
var user = AnyObject?()
This line of code came back as an error. The error message said
Cannot invoke initializer for type 'AnyObject?' with no arguments
Why do I receive this error and how do I fix it?
What you've written is the equivalent of:
var user = Optional<AnyObject>.init()
But Optional doesn't have an init() initializer. What are you trying to do?
If you really are trying to define a variable of type AnyObject? and you want to put an empty Optional<AnyObject> inside it you can simply write
var user: AnyObject? = .none
or as suggested by #Hamish
var user: AnyObject?