[英]Incompatible integer to pointer conversion assigning to 'CGFloat *' (aka 'float *') from 'int'
[英]Crash on Float to Int conversion
我的實時應用程序在 Float 到 Int 轉換線上隨機崩潰。
我無法弄清楚為什么它會崩潰並且無法重現它,因為它是實時應用程序上的隨機崩潰。 關於如何修復崩潰的任何建議?
崩潰日志
Crashed: com.apple.main-thread
0 MyApp 0x100703778 closure #1 in MyViewController.getBalance(isRefreshed:) + 797 (MyViewController.swift:797)
1 MyApp 0x1005c7670 closure #1 in closure #1 in fetchtBalance(completed:) + 185 (Utils.swift:185)
2 MyApp 0x10051ab10 thunk for @escaping @callee_guaranteed () -> () + 4335790864 (<compiler-generated>:4335790864)
3 libdispatch.dylib 0x1ae908b7c _dispatch_call_block_and_release + 32
4 libdispatch.dylib 0x1ae909fd8 _dispatch_client_callout + 20
5 libdispatch.dylib 0x1ae915cc8 _dispatch_main_queue_callback_4CF + 968
6 CoreFoundation 0x1aebdee0c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
7 CoreFoundation 0x1aebd9b68 __CFRunLoopRun + 1980
8 CoreFoundation 0x1aebd9084 CFRunLoopRunSpecific + 480
9 GraphicsServices 0x1b8e27534 GSEventRunModal + 108
10 UIKitCore 0x1b2d49670 UIApplicationMain + 1940
11 MyApp 0x1004ec6b8 main + 20 (ClientMyProfileViewController.swift:20)
12 libdyld.dylib 0x1aea58e18 start + 4
代碼
import UIKit
var balance: String!
var cost: String?
func getBalance () {
fetchBalance() { (result, error) in
guard let balanceFloatValue = Float(balance!) else {
print("Error")
return
}
guard let costFloat = Float(cost!) else {
print("Error")
return
}
let costPerSec = costFloat / 60
let talkTime = balanceFloatValue / costPerSec
let talkTimeInt = Int(talkTime) // Line No. 797 Crash here
// ....
}
}
此錯誤的最可能原因是值本身
let a:Float = 9999999999999999999999.9
let b = "0.000000000000000000001"
let c:Float = a/Float(b)!
let x = Int(c) // <- Crash here
0/0 會讓你崩潰: let c:Float = a/Float(b)! 所以這不是它,但任何其他可以使通話時間在 Int 之外的東西都會讓你崩潰這也可以解釋為什么你只會隨機遇到錯誤,因為它只適用於使除法 NaN 或 inf 或 int 之外的東西的值,如 bg2b說過
我認為您正在強制展開余額值。 嘗試解開轉換后的值,如:„Float(balance)!“
您可以像這樣快速將浮點值轉換為整數值:
var myIntValue = Int(myFloatValue)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.