Value of Optional Type 'Nsurl' Not Unwrapped; Did You Mean to Use '!' or ''

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

lastPathComponent returns an optional String:

Sample Image

but your RecordedAudio seems to require String not String?.
There are two easy ways to fix it:

Add ! in case you are sure lastPathComponent will never return nil

recordedAudio = RecordedAudio(title: recorder.url.lastPathComponent!, filePathUrl: recorder.url)

or

Use a default title in case lastPathComponent is nil

recordedAudio = RecordedAudio(title: recorder.url.lastPathComponent ?? "Default title", filePathUrl: recorder.url)

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 'NSURL?' not unwrapped; did you mean to use '!' or '?'?

ok I just unwrapped the optional using '!' and the code is compiling now.

return NSManagedObjectModel(contentsOfURL: modelURL!)

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.

Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?' Using Parse In Swift 2.0

The error message is pretty much self-explanatory, you need to unwrap the optionals. outlets are optionals so both usernameField.text and passwordField.text return a type String? (The type is optional string, not string) so you can't do any string related unless you unwrap it to be String. here is how you can do that

if  let username = self.usernameField.text, password = self.passwordField.text {
//your code
}

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.

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

The issue is that there is no guarantee that the user has selected a row, so your call to indexPathForSelectedRow is an optional (it may or may not have a value).

A way to improve this method is to use the guard statement to safely unwrap the two optional values you have in this method. In the event that one of them is not set (nil), the method will safely exit without crashing your app.

One benefit of using guard over the if let ... approach is that you can avoid a pyramid of doom. Using your example, it would require three indents to run the final command where you assign the string, thus making your code harder to read. The guard statement is explicitly saying "if this value fails, guard against crashing".

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "detailView") {
guard let vc = segue.destination as? ViewController else {
return
}

//Get the Index of selected Cell
guard let indexPath = self.tableView.indexPathForSelectedRow else {
return
}

//assign string to next view controller instance from selected cell.
vc.FirstString = FirstTableArray[(indexPath as NSIndexPath).row]
}
}

Also, two minor code style musings:

  • You do not need to use : NSIndexPath when you assign to indexPath. The compiler can infer the type for you
  • When declaring variables, iOS convention is to use camelCase, so while your indexPath variable is good, you should change the FirstString to firstString


Related Topics



Leave a reply



Submit