首页 > 解决方案 > 如何将 NSTextField 值转换为 CGFloat

问题描述

let countY = CGFloat(self.textbox1.stringValue); //NSTextField value= 1024.231234
if let image:CGImage = CGDisplayCreateImage(CGMainDisplayID(), rect: CGRect(x: countX, y: countY, width: 1, height: 1))
        {

我得到了错误

表达式类型“@lvalue String”在没有更多上下文的情况下不明确谢谢

标签: swiftcocoa

解决方案


CGFloat没有字符串初始值设定项。

只需doubleValue从文本字段中获取,而不是stringValue

let countY = CGFloat(self.textbox1.doubleValue)
if let image = CGDisplayCreateImage(CGMainDisplayID(), rect: CGRect(x: countX, y: countY, width: 1, height: 1)) {

}

推荐阅读