Questions tagged [forced-unwrapping]
100 questions
541
votes
23 answers
What does an exclamation mark mean in the Swift language?
The Swift Programming Language guide has the following example:
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { println("\(name) is being deinitialized") }
}
class…

Troy
- 21,172
- 20
- 74
- 103
40
votes
3 answers
Treating a forced downcast as optional will never produce 'nil'
I've been playing around with Swift and discovered that when down casting an object to be inserted into a dictionary, I get a weird warning: Treating a forced downcast to 'String' as optional will never produce 'nil'. If I replace as with as? then…

Pamelloes
- 690
- 1
- 6
- 13
29
votes
4 answers
Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals
I was very confused about forced unwrapping and implicit unwrapping at first. Now, the following understanding comes from my self-study:
There is no action available for implicit unwrapping, but there is something called implicitly unwrapped…

SLN
- 4,772
- 2
- 38
- 79
8
votes
5 answers
Swift safely unwrapping optinal strings and ints
When I am about to fire my segue for the 2nd view I also send some values like this:
if let aTime = ads[indexPath.row]["unix_t"].int {
toView.time = aTime
}
if let aTitle = ads[indexPath.row]["title"].string {
toView.title = aTitle
}
In…

user2636197
- 3,982
- 9
- 48
- 69
7
votes
1 answer
Is there a way to detect forced unwrapping across a Swift project?
Is there a way (via a compiler flag or a script) to detect forced unwraps across a Swift project?
I'm thinking about stuff like these:
let b = a as! B
let c = a!
a!.method()
Without triggering false-positives for var a: A! for instance.

ldiqual
- 15,015
- 6
- 52
- 90
7
votes
1 answer
Scala: Access optional value in optional object
Is there a good way to access a Option Value inside a Option Object?
The nested match cases result in a ugly tree structure.
So if I have for example:
case class MyObject(value: Option[Int])
val optionObject : Option[MyObject] =…

lukaswelte
- 2,951
- 1
- 23
- 45
5
votes
4 answers
Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used
I have a the following code which i am trying to use to initialize a variable and perform some operation on it.
let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+"…

Erent
- 571
- 6
- 15
- 29
5
votes
1 answer
Why does `+=` not work with implicitly unwrapped optionals
Why does += not work with implicitly unwrapped optionals, for instance:
var count: Int! = 10
count = count + 10 // This works
count += 10 // this does not work
Why isn't the optional implicitly unwrapped, like the case of count = count + 10 ?

Muhammad Zubair Ghori
- 683
- 1
- 6
- 12
5
votes
2 answers
Purpose of forced unwrapping
In swift documentation, you can find this :
if convertedNumber != nil {
println("convertedNumber has an integer value of \(convertedNumber!).")
}
// prints "convertedNumber has an integer value of 123."
With this explaination
Once you’re sure…

Tancrede Chazallet
- 7,035
- 6
- 41
- 62
4
votes
1 answer
Default value for optional type 'URL?'
I have an extension allowing me to download images to my UITableView from API.
extension UIImageView {
func donwloadImage(from url: String) {
let urlRequest = URLRequest(url: URL(string: url)!)
let task =…

Russ
- 99
- 1
- 7
4
votes
4 answers
Assigning text field value to variable in Swift
I am trying to learn Swift and it is turning out to be more different from other languages than I expected...
I just want to store the value of a user's input as an integer in a variable.
My attempts result in the following error:
"fatal error:…

Darth Washington
- 43
- 1
- 4
4
votes
1 answer
Why does SwiftyJSON create implicitly unwrapped optional for its constants?
In SwiftlyJSON's code, it defines the following constants using forced unwrapping:
///Error code
public let ErrorUnsupportedType: Int! = 999
public let ErrorIndexOutOfBounds: Int! = 900
public let ErrorWrongType: Int! = 901
public let ErrorNotExist:…

Boon
- 40,656
- 60
- 209
- 315
4
votes
1 answer
Early return/golden path in Swift
I'm used to write code with early return/golden path in Objective-C. I tried this approach in Swift, and noticed that early return comes at the expense of using the forced unwrapping operator (!) when optionals are involved.
Take a method that…

hpique
- 119,096
- 131
- 338
- 476
3
votes
2 answers
Need help understanding interaction between convenience initializer and instance variable unwrapping in Swift
I'm pouring over the Swift documentation about initializers and unwrapping and I'm failing to grok something fundamental about the way this works.
Goal: I'd like to subclass SKShapeNode and use let reverse : Bool to define an instance variable that…

thebossman
- 4,598
- 11
- 34
- 45
3
votes
1 answer
Swift4: Forced cast from 'Data?' to 'Data' only unwraps optionals; did you meant to use '!'?
at the last update to Swift 4, im always getting the same error at the same spot and i don't know how to clear it...
if tried to use ! instead of ? but the error keep going in the opposite direction.
the Error is with both Date? and Data?
thats the…

Sjero Markus
- 111
- 8