1
我需要任何CGFloat。如何將任何轉換爲Swift 3中的CGFloat
但是,我得到了一個致命的錯誤。 爲什麼不能將任何指定給CGFloat?如何轉換?
let lx = ix as! CGFloat
錯誤消息:
線程1:EXC_BAD_INSTRUCTION(代碼= EXC_l386_INVOP,子碼=爲0x0)
0x1040dcbf1 < 1329>:MOVSD -0xc0(%RBP),%XMM0; XMM0 = MEM [0],零
打印日誌:
CGFloat的:-0.5
CGFloat的:-0.5
代碼:
import Foundation
import UIKit
// creation of blocks
class Block {
var blockRect = [Rectangle]()
var type : String = String()
var w : Int = Int()
var h : Int = Int()
var start_x = CGFloat()
var start_y = CGFloat()
var start_w_offset : Int? = Int()
var blockView = UIView()
func unwrap<T>(_ any: T) -> Any
{
let mirror = Mirror(reflecting: any)
guard mirror.displayStyle == .optional, let first = mirror.children.first else {
return any
}
return first.value
}
init(map : [NSDictionary], color : UIColor, view : UIView, block_view : UIView, x: CGFloat, w: Int, h: Int, type: String, start_w_offset: Int?) {
self.blockView = block_view
for i in map {
let ix = unwrap(i["x"])
let iy = unwrap(i["y"])
print("CGFloat: \(ix)")
print("CGFloat: \(iy)")
let lx = ix as! CGFloat
let ly = iy as! CGFloat
let rd_x = x + lx * (size + padding)
let rd_y = 450 + ly * (size + padding)
let inner_x = block_view.frame.width/2 + lx * (size + padding)
let inner_y = block_view.frame.height/2 + ly * (size + padding)
self.start_x = x
self.start_y = 0
let r = Rectangle(type: "special", x: inner_x, y: inner_y, size: size, color: color, view: view, real_x : rd_x, real_y : rd_y)
self.blockRect.append(r)
self.type = type
self.w = w
self.start_w_offset = start_w_offset
self.h = h
}
}
}