[英]Swift using NSStatusBar statusItemWithLength and NSVariableStatusItemLength
I'm trying to rewrite the following code from the Status Bar Programming Topics in Swift. 我正在尝试从Swift中的状态栏编程主题重写以下代码。
NSStatusBar *bar = [NSStatusBar systemStatusBar];
theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[theItem retain];
[theItem setTitle: NSLocalizedString(@"Tablet",@"")];
...
My Swift code so far: 我的Swift代码到目前为止:
let bar = NSStatusBar.systemStatusBar()
let sm = bar.statusItemWithLength(NSVariableStatusItemLength)
sm.title = "Tablet"
...
The problem is that the statusItemWithLength
method in Swift excepts CGFloat
but NSVariableStatusItemLength
is defined as CInt
in Swift. 问题是Swift中的statusItemWithLength
方法除了CGFloat
但NSVariableStatusItemLength
在Swift中被定义为CInt
。 I see the following error: 我看到以下错误:
'CInt' is not convertible to 'CGFloat'
Definition in Xcode: Xcode中的定义:
var NSVariableStatusItemLength: CInt { get }
var NSSquareStatusItemLength: CInt { get }
class NSStatusBar : NSObject {
class func systemStatusBar() -> NSStatusBar!
func statusItemWithLength(length: CGFloat) -> NSStatusItem!
...
}
Am I doing something wrong? 难道我做错了什么? How can I fix this? 我怎样才能解决这个问题?
For Beta 1 & 2 you can manually convert NSVariableStatusItemLength from CInt to the required CGFloat like so: 对于Beta 1和2,您可以手动将NSVariableStatusItemLength从CInt转换为所需的CGFloat,如下所示:
let sm = bar.statusItemWithLength( CGFloat(NSVariableStatusItemLength) )
In Beta 3 NSVariableStatusItemLength is now a CGFloat, but due to a linker error (bug) you have to use 在Beta 3中,NSVariableStatusItemLength现在是CGFloat,但由于链接器错误(bug),您必须使用
-1 instead of NSVariableStatusItemLength and -1而不是NSVariableStatusItemLength和
-2 instead of NSSquareStatusItemLength -2而不是NSSquareStatusItemLength
let sm = bar.statusItemWithLength( -1 )
Thanks to suzhi and gui_dos for figuring this out! 感谢suzhi和gui_dos搞清楚这一点!
As a workaround, with the Beta 3 release you can pass the Int constant directly. 作为一种解决方法,使用Beta 3版本,您可以直接传递Int常量。 For instance: 例如:
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1) // NSVariableStatusItemLength
https://github.com/gui-dos/Guigna/blob/181f9db1056dece888dc29424cc2da79f8f284e3/Guigna-Swift/Guigna/GuignaAppDelegate.swift#L138 https://github.com/gui-dos/Guigna/blob/181f9db1056dece888dc29424cc2da79f8f284e3/Guigna-Swift/Guigna/GuignaAppDelegate.swift#L138
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.