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

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.

Related

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

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.

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.

What does .text and the dot mean in Swift? [duplicate]

This question already has answers here:
Calling a Swift class factory method with leading dot notation?
(3 answers)
Closed 5 years ago.
I am currently coding my own app and I was looking at a coding video for help and the person in the video was using .text and some other things that had dots in front of them like .color and .shape, which were variables declared and initialized already. I was wondering if anybody has a clear definition on what the .text means or what it does and what the overall dot means whenever you use it.
This feature is called "Implicit Member Expression"
An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a class method, in a context where type inference can determine the implied type. It has the following form:
.member name
For example : if you want to pick a color, in Swift4 you can simply do so:
let color: UIColor = .green
instead of typing :
let color : UIColor = UIColor.green()

Swift unwrapping Optional value by "?" and "!" [duplicate]

This question already has an answer here:
What is the difference when change to use optional chaining replace forced unwrapping in swift?
(1 answer)
Closed 5 years ago.
What are the differences when i unwrap Optional value by ? and !
If there is Class name A and there is a property name proA
Then if i make an instance like this
var a:A? = A()
a?.proA
a!.proA
I can approach property 'proA' two ways above. I know I cannot get value if I use a.proA because a is a Optional Type.
Also, I know a!.proA can get value because it unwrap the value in force
And what is a?.proA?
What are the differences between a?.proA and a!.proA
As you stated, ! forcefully unwraps the optional.
var a: A? = A()
a!.prop
However, what happens if the optional was never assigned a value, which means it's nil?
var a: A?;
a!.pop
You get a Swift runtime error (think of it as the equivalent of a null pointer exception in other languages e.g. Java).
On the other hand, if you use ?, you can take advantage of optional chaining. This allows you to gracefully traverse optional properties without fear of causing a runtime error. If some intermediate property along the chain is nil, the whole expression will return nil.
var a: A?;
a?.pop //returns nil, no runtime error

Is casting the same as converting in swift? [duplicate]

This question already has an answer here:
Typecasting or initialization, which is better in Swift?
(1 answer)
Closed 6 years ago.
When stumbling across casting using "as! or as?", I also noticed that types could also be converted using the desired type inside of parenthesis such as:
let x : Int = 42
var myString = String(x)
This made me curious to ask if converting and casting are the same? However when I tried to do converting in a another example using a reference type, I don't think the compiler allows this or at least it gave me an error, such as:
let sutCast = storyboard.instantiateViewController(withIdentifier: "ItemListViewController") as! ItemListViewController
let sutConvert = ItemListViewController(storyboard.instantiateViewController(withIdentifier: "ItemListViewController"))
Is it safe to say or assume that in Swift, conversions are not allowed for reference types and casting is different from conversion because it depends if an object is a reference type or a value type?
From the documentation:
Type casting is a way to check the type of an instance, or to treat that instance as a different superclass or subclass from somewhere else in its own class hierarchy.
It is a way to convert one type to another but it can also be used for more, such as to check the type etc.
Refer to the documentation for more info.