【问题标题】:Apple Match-O Linker (id) Error _NSVariableStatusItemLengthApple Match-O 链接器 (id) 错误 _NSVariableStatusItemLength
【发布时间】:2014-11-30 12:46:04
【问题描述】:

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

这里是错误扩展:

最后这是我的代码

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 展示了如何制作状态栏应用程序!

【问题讨论】:

    标签: xcode swift compiler-errors


    【解决方案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 网站上发布这篇文章的人。

    【讨论】:

      猜你喜欢
      • 2014-12-30
      • 2016-01-09
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2011-12-06
      相关资源
      最近更新 更多