Swift 2: !, ? -" Value of Optional Type "..." Not Unwrapped"

Value of optional type 'Float?' not unwrapped; did you mean to use '!' or '?'?

you can solve like that

topViewController.moduleWeight = detailItem?.value ?? 0

or change moduleWeight as optional

var moduleWeight : Float?

and use it normal

topViewController.moduleWeight = detailItem?.value

Value of optional type 'Date?' not unwrapped; did you mean to use '!' or '?'? In Swift 4 XCode 9

self.selectedEntity? is an optional. All of it's values are also optional. See Optional Chaining in the Swift documentation for more information. You need to unwrap your optionals before using them as non-optional objects. That's what you are doing when you use the ! symbol here self.selectedEntity?.dob!. That force unwraps the optional and if it is nil, your app will crash. Your self.dob.date object is not an optional so you have to unwrap your optional self.selectedEntity?.dob object before using it. You can safely unwrap your self.selectedEntity object with something like this.

if let selectedEntity = selectedEntity {
self.dob.date = selectedEntity.dob
}

Swift 2: !, ? - Value of optional type ... not unwrapped

obj?.fn() calls fn member function if the object isn't null, otherwise doesn't do anything.

obj!.fn() on the other hand asserts that obj isn't null, and calls fn. If the object is null, you get an exception.

So it's a difference in assertiveness: you either ask or simply claim the nullable property of a nullable object.

Swift: Value of optional type '(Int, Int)?' not unwrapped; did you mean to use '!' or '?'?

The problem is being caused by these lines:

let x = self.coordinates[lines[index][0]]
let y = self.coordinates[lines[index][1]]
let z = self.coordinates[lines[index][2]]

x, y, and z are all optional because a dictionary always returns an optional when looking up a value by key because the key might not exist even if you know they do.

One solution is to do:

if let x = self.coordinates[lines[index][0]], let y = self.coordinates[lines[index][1]], let z = self.coordinates[lines[index][2]] {
if self.board[x.0][x.1] != 0 && self.board[x.0][x.1] == self.board[y.0][y.1] && self.board[x.0][x.1] == self.board[z.0][z.1] {
return self.board[x.0][x.1]
}
}

This ensures that x, y, and z are properly unwrapped.

Another option would be to use the ?? operator. Change:

let x = self.coordinates[lines[index][0]]

to something line:

let x = self.coordinates[lines[index][0]] ?? (0, 0)

Do this for y and z as well. Then you won't get the error.

Issue with Swift: value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?'?

Because dictionary look ups return an optional (the key might be missing), you need to unwrap the result of next[storyIndex] before you can index it. Use ? (optional chaining) here to safely unwrap the value. Since you need the result, instead of comparing to nil, use if let (optional binding):

if let nextStory = next[storyIndex]?[myTag] {
storyIndex = nextStory
}

If storyIndex is not a valid key, then next[storyIndex] will be nil, and the result of the optional chain will be nil. If myTag is not a valid key, the result will also be nil. In the case that both keys are valid, the result of the optional chain will be Int? and nextStory will be bound to the unwrapped value.


If you have a default value to use for storyIndex (such as 1) if the look ups fail, you can use the nil coalescing operator ?? also with the optional chain to do this in one line:

storyIndex = next[storyIndex]?[myTag] ?? 1

or (to leave storyIndex unchanged for lookup failure):

storyIndex = next[storyIndex]?[myTag] ?? storyIndex

if let not unwrapping an optional value in Swift 4.0

Short answer:

You are creating a doubly-wrapped optional because of the combination of optional chaining and the use of try?. So, if let is only unwrapping the first layer of doubly-wrapped optional leaving you with an optional variable.


Why is this?

try? is used with a call that throws. If the call does throw an error, the expression returns nil. If the call succeeds, the value is wrapped in an optional.

You are using an optional chain to call prediction on model. If model is nil, then the result of the optional chain is nil. Note, this result didn't throw so the result gets wrapped in an optional by try? resulting in Optional(nil) which is not the same as nil. If the call to prediction succeeds, the result gets wrapped in an optional because of the optional chain, and wrapped in an optional again because of the try?.

So there are three possible results:

  1. Model is not nil and call to prediction returns a valid result of type Inceptionv3Output without throwing. This result becomes Inceptionv3Output? because of the optional chain and Inceptionv3Output?? because of the try?.
  2. Model is not nil, but the call to prediction throws an error. The result of the try? is nil because of the throw.
  3. Model is nil. The result of the optional chain is nil, and that result gets wrapped in an optional again because of try ? resulting in Optional(nil).

So, your if let is unwrapping a value of type Inceptionv3Output?? which means prediction has the type Inceptionv3Output?. The if let succeeds if the call doesn't throw. If the model is nil, then prediction will be nil, otherwise it contains the wrapped value which is the result of the prediction call on the model.

Learning Swift, Value of optional type not unwrapped when creating an object

Both the text property of UITextField and the result of the Int(string:String) initializer return an optional value.

So you have to unwrap both in case the method expects non optional parameters.

let novaRefeicao = Meal(nomeAlimento: nomeRefeicao!, alegriaEmComer: valorFelicidadeInt!)

The most compact syntax is

class ViewController: UIViewController {

@IBOutlet weak var refeicaoField: UITextField!
@IBOutlet weak var felicidadeField: UITextField!


@IBAction func addRefeicao(sender: UIButton) {

if let nomeRefeicao = refeicaoField.text, valorFelicidade = felicidadeField.text, valorFelicidadeInt = Int(valorFelicidade) {
let novaRefeicao = Meal(nomeAlimento: nomeRefeicao, alegriaEmComer: valorFelicidadeInt)
// do something with novaRefeicao
}
}
}

The Meal initializer is only called if

  • refeicaoField.text is not nil
  • felicidadeField.text is not nil and can be converted to Int

There is no need to check the UITextField instances for nil.

If you get a runtime error, one of the fields is not connected in IB.

The message Initialization of immutable value ... is a warning, because the created Meal instance assigned to novaRefeicao is never used. That's why I added the comment line do something …

Value of optional type 'Any?' not unwrapped

You need to cast type Any to Swift dictionary type, you can do it in this way:

if let userInfo = result as? [String: Any] {
let email = userInfo["email"] as? String
}


Related Topics



Leave a reply



Submit