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()
Related
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.
This question already has answers here:
swift difference between final var and non-final var | final let and non-final let
(2 answers)
What is the difference between final Class and Class?
(7 answers)
Closed 3 years ago.
A newbie question.
I'm reading swift documentation AccessControl. It says there are 5 access specifiers.
Open
Public
Internal
File-private
Private
I thought Final is also an access specifier. If not what is it? And can someone give link to the documentation?
It's not an Access Control Specifier, it's a Declaration Modifier.
final
Apply this modifier to a class or to a property, method, or subscript member of a class. It’s applied to a class to indicate that the class can’t be subclassed. It’s applied to a property, method, or subscript of a class to indicate that a class member can’t be overridden in any subclass.
Source: Swift Language Reference - Declaration – Declaration Modifiers
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.
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.
This question already has answers here:
Cannot assign property in method of struct
(2 answers)
Closed 7 years ago.
I'm studying swift and I'm trying to understand the difference between classes and struct, which as I understand it, is in these two points:
1) Classes are reference types, Whereas structures are value types. That means That When you pass an instance of a structure to a function, return an instance from a function, or assign the value of a variable That Refers to a structure to another variable, the instance is copied. In other words, structures exhibit pass-by-value behavior. Swift strings, arrays, and dictionaries are all Implemented as structures.
By contrast, class instances are passed by reference-no copy is taken.
2) Classes can be subclassed to add behavior; structures can not.
why these two errors?
depends on the structure ?, because a class does not give me the same error.
The reason you're getting those errors is that by default, the properties of a value type (i.e. a struct) cannot be modified from within its instance methods (e.g. your newRad() and plusOne() methods).