All Questions
Tagged with lazy-initialization swift
71 questions
1
vote
0
answers
68
views
Convert Realm objects to non-third-party objects in a lazy way: Results<Item> to [Item]
I'm trying to map Realm Persisted Objects to another Type for separation of concerns.
I have something like a protocol repository retrieving an array of items: [Item]
I've one implementation of this ...
0
votes
1
answer
151
views
Swift can lazy, get and didSet together
Can we do get and didSet together with lazy keyword. I have an array it fetch data from database (get) and another thing when that array will modify i will call delegate to reload data in didSet.
Its ...
0
votes
1
answer
178
views
Lazy var initialization error "Cannot use mutating getter on immutable value"
I tried two ways of initializing a lazy var, one works, the other gets a compiler error.
OK: var maxDiscount = MaxDiscount(); maxDiscount.maxDiscountPercent
ERROR: MaxDiscount().maxDiscountPercent
If ...
1
vote
2
answers
107
views
Whats the difference of using ={}() with a lazy property or just =
There are two different ways of using the lazy property syntax and I fail to see the difference between them:
//1
lazy var a = { "hello" }()
//2
lazy var b = "hello"
In other ...
1
vote
1
answer
89
views
Why this code capturing self inside this block compile in swift?
I work in a project were I came across multiples cases like this,
class ViewController: UIViewController {
private let clearCacheButton: UIButton = {
let button = UIButton(type: .custom)
...
0
votes
2
answers
836
views
Accessing a lazy property on a struct mutates the struct
I have a lazy property in a struct and every time I access it, it mutates the struct.
var numbers = [1,2,3]
struct MyStruct {
lazy var items = numbers
}
class MyClass {
var ...
0
votes
1
answer
75
views
Initialization problem with Swift struct that contains lazy initializers
I wrote two versions of code for a Swift programming exercise from an online programming exercise website. The exercise is as follows:
Find the difference between the square of the sum and the sum of ...
0
votes
0
answers
57
views
How do I create a lazy property without getting errors?
Trying to get the hang of creating lazy class properties. Sometimes it works, other times I get errors and I'm not sure I understand when it's okey to do it and when its not. For example, I created ...
0
votes
1
answer
681
views
How the global variable / constants are lazy in swift
From the docs.swift.org
Global variable
Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties. Unlike lazy stored properties, global constants and ...
1
vote
1
answer
72
views
Is it safe (or good programming style) to initialize a lazy var with nil vars?
Let me explain a little better what I mean since it's kinda tricky to understand.
I'm creating a prototype for a videogame. Every level inherits the main rules from a SKScene called SceneLogic:
...
0
votes
1
answer
169
views
What is the difference between lazy var normal instantiation and closure instantiation?
lazy var propertyOne: BabyYoda = BabyYoda(delegate: self.mandalorian)
vs.
lazy var propertyTwo: BabyYoda = {return BabyYoda(delegate: self.mandalorian)}()
Is one more efficient than the other? Do ...
0
votes
1
answer
757
views
Setting a variable to a defined lazy var within a UIView closure cause reference problems
Recently, I was working on a project of mine and I wanted to have multiple labels with the same font, text color, and properties, except their text.
This is the code I wrote:
lazy var profileLabel: ...
7
votes
1
answer
4k
views
Is Property Wrapper @Lazy variable thread safe?
We now have a new way to make a lazy variable. It is described in swift-evolution/proposals/0258-property-wrappers.md:
@propertyWrapper
enum Lazy<Value> {
case uninitialized(() -> Value)
...
0
votes
3
answers
2k
views
Why I get error "cannot assign value of type (class) to type UICollectionViewDelegate, UICollectionViewDataSource?"
When I declare my collection view, I got error "cannot assign value of type (class) to type UICollectionViewDelegate, UICollectionViewDataSource":
let collectionView: UICollectionView = {
let ...
0
votes
2
answers
1k
views
How to return from completion handler inside a lazy set var
I want to fetch api to get content from server inside lazy var. Following is my code, I'm not sure how to get it working. Any clue? Im aware that we can't return from completion handler so I'm lost ...