Value of Optional Type Cgfloat Not Unwrapped Error in Swift

Value of optional type CGFloat not unwrapped error in Swift

You are using optional chaining, which means that self.window?.frame.width evaluates to a valid integer if window is not nil, otherwise it evaluates to nil - same for height.

Since you cannot make a CGRect containing nil (or better said, CGRectMake doesn't accept an optional for any of its arguments), the compiler reports that as an error.

The solution is to implicitly unwrap self.window:

    var loadView = UIView(frame: CGRectMake(0, 0, self.window!.frame.size.width, self.window!.frame.size.height))

But that raises a runtime error in the event that self.window is nil. It's always a better practice to surround that with a optional binding:

if let window = self.window {
var loadView = UIView(frame: CGRectMake(0, 0, window.frame.size.width, window.frame.size.height))
.... do something with loadVIew
}

so that you are sure the view is created only if self.window is actually not nil.

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
}

Value of optional type String? not unwrapped

Because theButton.titleLabel?.text it not unwrapped.

You can use

 answerField.text  = answerField.text + (theButton.titleLabel?.text ?? "")

or

if answerField.text == "0" {
answerField.text = theButton.titleLabel?.text
} else {
if let txt = theButton.titleLabel?.text {
answerField.text = answerField.text + txt
} else {
answerField.text = answerField.text
}
}

If let not still giving Optional must be unwrapped error

As mentioned into documents for jpegData(compressionQuality:)

Declaration

func jpegData(compressionQuality: CGFloat) -> Data?

Return Value

A data object containing the JPEG data, or nil if there was a problem
generating the data. This function may return nil if the image has no
data or if the underlying CGImageRef contains data in an unsupported
bitmap format.

So basically it's not img object which is causing this error. but it's return value from jpegData property of UIImage.

You can use guard let or if let to get the explicit value from jpegData property.

Value of optional Type AnyObject? not unwrapped did you mean to use ! or?

Unwrap values via if let or guard let statements:

guard let cityDetails = response["cityDetails"] as? NSArray else {
// Something went wrong trying to turn response["cityDetails"] into an unwrapped NSArray
return
}

// From this point cityDetails will be an unwrapped NSArray

Forced unwrapping of variables is a thing you should always try to avoid whenever possible.

The other snippet:

 guard let result = result  // Unwrap result
where result.isKindOfClass(NSArray) else { // Ask the unwrapped value if it's an NSArray
return // If one of above is not the case we shouldn't continue
}

Cannot force unwrap value of non-optional type: Avoid Optional()

You've already unwrapped playerDic in the if let binding. Just drop the force unwrap like the error message tells you.

if let playerDic = defaults.objectForKey("Player") as? [String: Int] {
lbLevel.setText(String(playerDic["level"]))
}

Update
Sorry just saw your update. So playerDic isn't an optional, but the values returned for keys are optionals. If you ask for the value for a key that is not in the dictionary you will get an optional with the value of nil.

if let 
playerDic = defaults.objectForKey("Player") as? [String: Int],
level = playerDic["level"] {
lbLevel.setText("\(level)")
}

Here you can bind multiple values in a single if let. Also, you can use String(level) or use string interpolation "(level)" depending on what you prefer.

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

Oh, wait, I think the problem (maybe) is that the assignment is being interpreted as higher precedence than the comparison. The compiler doesn't know whether the comparison is supposed to be for the entire assignment or just the expression for the assignment. Try adding parentheses:

extension UIFont {
func sizeOfString (string: String, maxWidth w: CGFloat?, maxHeight h: CGFloat?) -> CGSize {
var width = (w != nil ? w! : DBL_MAX) // Error on this line
var height = (h != nil ? h! : DBL_MAX) // And on this line
var size = CGSize(width: width, height: height)
var options = NSStringDrawingOptions.UsesLineFragmentOrigin
var attributes = [NSFontAttributeName: self]
var nsstring = NSString(string: string)
return nsstring.boundingRectWithSize(size, options: options, attributes: attributes, context: nil).size
}
}

I think that without the parentheses, the assignments are being interpreted as:

    (var width = w) != nil ? w! : DBL_MAX // Error on this line


Related Topics



Leave a reply



Submit