Apple Match-O链接器(id)错误_NSVariableStatusItemLength

时间:2014-11-30 12:46:05

标签: xcode swift compiler-errors

我正在尝试在swift中创建一个状态栏项目,以便我可以轻松监控电池,但是出现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
    }


}

我已尝试按照保罗回答this question时的建议删除DerivedData目录来修复此问题,但这无效。

修改 感谢@lemonmojo展示如何制作状态栏应用程序!

1 个答案:

答案 0 :(得分:1)

我刚刚发现了如何解决这个问题。要制作菜单,我必须将一些代码改为此。

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网站上发布此文章的人。