All Questions
Tagged with option-type swift
1,321
questions
-1
votes
1
answer
92
views
How to force non-optional type (for array element) on compile time in Swift? [closed]
struct MyStruct {
@ArrayOfNonOptionalElements var arr: [SomeNonOptionalType]
}
where @ArrayOfNonOptionalElements is a propertyWrapper.
So, how to force non-optionality for the type inside? Is it ...
2
votes
2
answers
90
views
Swift, Why are the memory sizes of 'String' and 'String?' the same?
Case Int, Int?
I realized that in the case of optional variables, a bit is needed to check if nil, so it comes out 1 byte larger.
But why is the memory size of String and String? the same?
Not only ...
-2
votes
1
answer
195
views
How to fix this force cast in swift
I am facing this below issue in my project, kindly help me to fix this issue.
Force Cast Violation: Force casts should be avoided. (force_cast)
var guestNetworks: [WanNetworks] {
for result in ...
1
vote
3
answers
263
views
guard/if let in Java - declare a property or an object if it can be unwrapped
In swift there is the guard let / if let pattern allowing us to declare an object or a property only if it can be unwrapped.
it works a follow:
func getMeaningOfLife() -> Int? {
42
}
func ...
-1
votes
1
answer
45
views
meaning of this code: return self.desk[number]![alpha]
please help me understand this part of the code. Why is ! located between two brackets []![] in this expression: self.desk[number]![alpha]?
class GameDesk {
var desk : [Int: [String: Chessman]] = [...
0
votes
1
answer
131
views
Initializer for conditional binding must have Optional type, not 'Float' Error in swift
Given:
var conv_factor: String?
var standard_rate_in_usd: Double?
now I need a Float value
var floatVal: Float?
if let priceVal = Float(indexData.standard_rate_in_usd!),
let convVal = Float((...
0
votes
0
answers
50
views
Binding an optional object from one view to be changed in another in SwiftUI [duplicate]
I have a top-level object Estate, which is changed throughout the app. And this object has an optional object Meter, which can also be changed by adding Reading. The binding of the Estate is possible ...
0
votes
1
answer
101
views
I tried to get the index of an element from Core Data, but it shows an error even I force unwrapped the value of the index
I try to use a function to sorted the data in the Core Data, and get the firstIndex of the first element base on the predicate, but when I used .firstIndex, it shows:
Value of type '((Filedetails) -&...
0
votes
1
answer
409
views
extension for Optional Task
Can we make an extension of Optional type where wrapped type is async Task which is generic struct itself? I al so need to use Task underlying types inside.
Example of what I need (which is not ...
-1
votes
2
answers
83
views
How to do optional with an or conditions in Swift?
i am trying to do following:-
var isSomeThingTrue = checkSomeThing()
if (let someOptional = getSomeOptional(),
let anotherOptional = getAnotherOptional(withSomeOptional: someOptional)) ||
...
0
votes
2
answers
95
views
Why call func from protocol instead of struct
protocol Readable {}
extension Readable {
func text() -> String? {
"in protocol"
}
}
struct Book: Readable {
func text() -> String {
"in struct"
...
0
votes
1
answer
439
views
Swift error with change from string to double (why String type number becomes nil?)
I'm trying to make my own toy project to study Swift.
But I got a problem with converting string to double.
Please refer to the following codes first.
//MARK: - ExchangeManagerDelegate
extension ...
0
votes
0
answers
96
views
fetch boolean core data and filter using if statement
in my swift code below the goal is to fetch a boolean value from core data. Once that value is fetched it should be filtered using a if statement. If the value is true is should display a image on the ...
0
votes
1
answer
153
views
Error to use a shorter spelling to unwrap a value
in Swift documentation at https://docs.swift.org/swift-book/GuidedTour/GuidedTour.html, there are examples about usage of optionals and unwrapping them. When I try the examples on my Macbook, I got an ...
0
votes
0
answers
166
views
fetch Boolean values from core data
In my swift code below the goal is to fetch a series of Boolean values. It should fetch multiple values. Something along the lines of false, false, true etc. Right now I am having issues in my base ...