Differencebetween ":" and "=" in Swift

What is difference between ? and ! in Swift?

Use attachment.image!.size if you're guaranteed that image? isn't nil. If you're wrong (and it is nil) your app will crash. This is called forced unwrapping.

If you're not sure it won't be nil, use image?.

In your case image! is OK, since you control whether placeholder.png exists.

Read all of the documentation on Swift Optionals. It's a basic and fundamental part of the language.

what is the difference between ? and ! operator in swift?

An optional type can be nil

var nameOfToy: String?

The toy may have a name;

nameOfToy = "Buzz"

so

print (nameOfToy!)

is Buzz.

The question mark indicates that it is optional, i.e. can be nil, so you have to UNWRAP it with the !. This is because the variable is optional.

But what happens if there is no name of a toy, and you use !. In this case you get a nasty crash.

Difference Between ? and ! in Swift Language?

As per the official Apple docs:

When working with optional values, you can write ? before operations
like methods, properties, and subscripting. If the value before the ?
is nil, everything after the ? is ignored and the value of the whole
expression is nil. Otherwise, the optional value is unwrapped, and
everything after the ? acts on the unwrapped value. In both cases, the
value of the whole expression is an optional value.

Once you’re sure that the optional does contain a value, you can
access its underlying value by adding an exclamation mark (!) to the
end of the optional’s name. The exclamation mark effectively says, “I
know that this optional definitely has a value; please use it.” This
is known as forced unwrapping of the optional’s value (...)

Apple Inc. 'The Swift Programming Language'. iBooks. https://itun.es/nl/jEUH0.l

What is the difference between the following 3 declarations?

1.

var title: UILabel {
let label = UILabel()
textLabel.font = .systemFontOfSize(13)
return label
}

It is a read only computed property. Computed properties cannot be let. These are calculated using other stored/computed properties. So they don't have any backing store of their own. Hence, computed properties are always declared as var.

2.

let title: UILabel = {
let label = UILabel()
textLabel.font = .systemFontOfSize(13)
return label
}()

It is a stored property. This is assigned a closure that returns a UILabel object. This closure is executed during the instantiation process of the object and the returned UILabel object is assigned to title.

3.

lazy var title: UILabel = {
let label = UILabel()
textLabel.font = .systemFontOfSize(13)
return label
}()

It is a lazy stored property. It is also assigned a closure that returns a UILabel object. But this closure is not executed during the instantiation process. It is executed whenever this property is first used. After the execution of the closure, the UILabel object returned is assigned to title.

Difference between computed property and property set with closure

In short, the first is a stored property that is initialized via a closure, with that closure being called only one time, when it is initialized. The second is a computed property whose get block is called every time you reference that property.


The stored property’s initialization closure is called once and only once, but you can later change the value of the stored property (unless you replace var with let). This is useful when you want to encapsulate the code to initialize a stored property in a single, concise block of code.

The computed property’s block, however, is called each time you reference the variable. It’s useful when you want the code to be called every time you reference the computed property. Generally you do this when the computed property needs to be recalculated every time you reference the stored property (e.g. recalculated from other, possibly private, stored properties).

In this case, you undoubtedly want the stored property (the first example), not the computed property (the second example). You presumably don't want a new push behavior object each time you reference the variable.


By the way, in your first example, you internally reference to it being instantiated lazily. If you want that behavior, you must use the lazy keyword:

lazy var pushBehavior: UIPushBehavior = {
let behavior = UIPushBehavior()
behavior.setAngle(50, magnitude: 50)
return behavior
}()

If, however, the property is static, it is automatically instantiated lazily.

What is difference between '&' and ',' in swift about Protocol Composition

From the Swift documentation on protocols, I think & is mainly used when you are requiring a variable to adopt to multiple protocols, ex:

func wishHappyBirthday(to celebrator: Named & Aged) {
print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}

And you use , when you want to adopt a custom struct/class to multiple protocols:

struct SomeStructure: FirstProtocol, AnotherProtocol {
// structure definition goes here
}


Related Topics



Leave a reply



Submit