Unexpected Behavior When Casting an Nsnumber to Float

Unexpected behavior when casting an NSNumber to Float

This is a consequence of SE-0170 NSNumber bridging and Numeric types, implemented in Swift 4:

as? for NSNumber should mean "Can I safely express the value stored in this opaque box called a NSNumber as the value I want?".

1.12 is a floating point literal, and inferred as a Double, so NSNumber(value: 1.12) is “boxing” the 64-bit floating point value
closest to 1.12. Converting that to a 32-bit Float does not
preserve this value:

let n = NSNumber(value: 1.12)
let x = Float(truncating: n) // Or: let x = n.floatValue
let nn = NSNumber(value: x)
print(n == nn) // false

On the other hand, 1.0 can be represented exactly as a Float:

let m = NSNumber(value: 1.0)
let y = m.floatValue
let mm = NSNumber(value: y)
print(m == mm) // true

and that is why casting m as? Float succeeds. Both

n.floatValue
Float(truncating: n)

can be used to ”truncate” the number to the closest representable
32-bit floating point value.

Swift Unable to bridge NSNumber to Float

I am able to reproduce this problem with this simple example:

var arr = [NSNumber(value: 1.3 as Float), NSNumber(value: 2.7)]

// This works
let x = arr.map { $0.floatValue }
print(x)
[1.3, 2.7]
// This fails with error 'Fatal error: Unable to bridge NSNumber to Float'
let y = arr as! [Float]
print(y)

As explained in the duplicate's answer, you can't cast an NSNumber to Float if the cast doesn't preserve the exact value. Double(2.7) loses precision when converted to Float, so the cast fails. Had I chosen 2.5 instead of 2.7 for the Double, the cast would have worked.

Accessing the value with .floatValue works because it uses the property of NSNumber to produce the Float value.

Unable to bridge NSNumber to Float Swift 3.3

As you have found, you may need to cast it first to NSNumber.

Something like this:

randomVariable = (jsonDict["jsonKey"] as? NSNumber)?.floatValue ?? 0

Maybe regex replacement would help you update your code.

Pattern: jsonDict\[([^\]]*)\] as\? Float

Replace with: (jsonDict[$1] as? NSNumber)?.floatValue

Objective-C cast a block type into another got unexpected result

Explain: Calling the block as blockType- (void (^)()), the block is treated as (void (^)(double)).

Resolve: Must cast the block back to (void (^)(float)) when invoking.

IQKeyboardManager unexpected behavior

The issue is with isEnable property used isUserInteration property instead of that

@IBAction func sendPressed(_ sender: AnyObject) {

//TODO: Send the message to Firebase and save it in our database

if (messageTextfield.text?.isEmpty)!{
showAlert(alertTitle: "", alertMessage: "can't send empty Message", actionTiltle: "Ok")
}
else{
messageTextfield.isUserInteractionEnabled=false
sendButton.isUserInteractionEnabled=false
let messageDB=Database.database().reference().child("Messages")
let dictionary:[String:String]=["Sender":(Auth.auth().currentUser?.email)!,"MessageBody":messageTextfield.text!]
messageDB.childByAutoId().setValue(dictionary)
messageTextfield.text=""
messageTextfield.isUserInteractionEnabled=true
sendButton.isUserInteractionEnabled=true

}

}


Related Topics



Leave a reply



Submit