gpt4 book ai didi

xcode - Apple Mach-O 链接器 (id) 错误 _NSVariableStatus 项目长度

转载 作者:行者123 更新时间:2023-11-28 07:13:53 24 4
gpt4 key购买 nike

我正在尝试快速制作一个状态栏项目,以便我可以轻松监控我的电池,但是我有一个 Apple Match-O Linker (id) Error _NSVariableStatusItemLength 错误。以下是有关该错误的一些屏幕截图:
Error1

这里是扩展的错误:
Error 2

最后是我的代码

import Cocoa
func executeCommand(command: String, args: [String]) -> String {

let task = NSTask()

task.launchPath = command
task.arguments = args

let pipe = NSPipe()
task.standardOutput = pipe
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)!

return output

}

extension Array {
func combine(separator: String) -> String{
var str : String = ""
for (idx, item) in enumerate(self) {
str += "\(item)"
if idx < self.count-1 {
str += separator
}
}
return str
}
}

extension String{
func replace(x:String,y:String) -> String{
var z = self.stringByReplacingOccurrencesOfString(x, withString: y, options: NSStringCompareOptions.LiteralSearch, range: nil)
return z
}
func toUnformatedInt() -> Int{
return (self.replace(",", y: "")).toInt()!
}
func split(delimiter:String) -> Array<String>{
return self.componentsSeparatedByString(delimiter)
}
}

extension Int{
func toFormatedStr() -> String{
var i = 0
var Str = ""
for x in String(self){
if i == 3{
Str += ",\(x)"
i = 1
} else {
Str += "\(x)"
i += 1
}
}
return Str
}
func toStr() -> String{
return String(self)
}
}

func get_key_and_value(string:String) -> Array<Any>{
var x = string.split(" = ")
if x[1].toInt() != nil{
return [x[0],x[1].toInt()!]
} else if x[1] == "Yes"{
return [x[0],true]
} else if x[1] == "No"{
return [x[0],false]
} else {
return [x[0],x[1]]
}
//return [return_val[0],return_val[1]]
}

class Battery{
var battery = Dictionary<String, Any>()
init(){
refreshValues()
}
internal func refreshValues(){
let commandOutput:String = executeCommand("/usr/sbin/ioreg", ["-r","-w0","-cAppleSmartBattery"])
var commandOutList = commandOutput.split("\n")

commandOutList.removeAtIndex(0)
commandOutList.removeAtIndex(0)

for _ in 1...5{
var lastRemoved = commandOutList.removeLast()
if lastRemoved == "}"{
break
}
}

//Got Needed Infomation Only

let commandOutStr = commandOutList.combine("")
let cmdOutStr = commandOutStr.replace("\"",y: "").replace(" ",y: "\n")

var cmdOutSplit = cmdOutStr.split("\n")
cmdOutSplit.removeAtIndex(0)


//var x = get_key_and_value(cmdOutSplit[0])
//var key:String = x[0] as String
//var value = x[1] as Any

for x in cmdOutSplit{
var y = get_key_and_value(x)
battery[(y[0] as String).replace(" ", y: "")] = y[1] as Any
}
}
}
@NSApplicationMain


class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var StatusMenu: NSMenu!
var statusItem: NSStatusItem?;

func applicationDidFinishLaunching(aNotification: NSNotification) {
let bar = NSStatusBar.systemStatusBar()
statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))
statusItem!.title = "Status Menu"
statusItem!.menu = StatusMenu
statusItem!.highlightMode = true
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}


}

我已尝试按照 Paul 回答 this question 时的建议删除 DerivedData 目录来解决问题。然而这并没有帮助。

编辑:感谢@lemonmojo 展示了如何制作状态栏应用程序!

最佳答案

我刚刚找到了解决这个问题的方法。为了制作菜单,我必须为此更改一些代码。

class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet var menu: NSMenu!


var statusItem: NSStatusItem!;

override func awakeFromNib() {


}

func applicationDidFinishLaunching(aNotification: NSNotification?) {

// Make a status bar that has variable length (as opposed to being a standard square size)

// This -1 should be 'NSVariableStatusItemLength' instead, but causes a link error
// PRODUCTION: Remind authors to check this before we leave early release - it may be fixed by Apple by then.

statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)

// Set the text that appears in the menu bar
statusItem.title = "My Item"

// Set the menu that should appear when the item is clicked

statusItem.menu = self.menu

// Set if the item should change colour when clicked
statusItem.highlightMode = true

}

我认为这个错误是因为我必须更改 statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
感谢在 this 上发布这篇文章的人网站。

关于xcode - Apple Mach-O 链接器 (id) 错误 _NSVariableStatus 项目长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27213377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com