Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 使用Swift 2在OSX上设置桌面背景_Macos_Swift_Cocoa_Swift2 - Fatal编程技术网

Macos 使用Swift 2在OSX上设置桌面背景

Macos 使用Swift 2在OSX上设置桌面背景,macos,swift,cocoa,swift2,Macos,Swift,Cocoa,Swift2,在介绍了Javascript之后,我试图通过创建一些简单的工具来解决OSX/iOS编程问题 然而,我一跳就遇到了路障 我找到了两个应该有效的例子 这是第二点: #!/usr/bin/env xcrun swift import Foundation import AppKit let imagesDir = "/Users/david/Dropbox/Graphics/Wallpaper-HD/" var err: NSError? let fs = NSFileManager.defa

在介绍了Javascript之后,我试图通过创建一些简单的工具来解决OSX/iOS编程问题

然而,我一跳就遇到了路障

我找到了两个应该有效的例子

  • 这是第二点:

    #!/usr/bin/env xcrun swift
    
    import Foundation
    import AppKit
    let imagesDir = "/Users/david/Dropbox/Graphics/Wallpaper-HD/"
    var err: NSError?
    
    let fs = NSFileManager.defaultManager()
    let filenames = fs.contentsOfDirectoryAtPath(imagesDir, error: &err) as [String]?
    
    if let error = err {
      NSLog(error.localizedDescription)
    } else {
    
      let imagenames = filenames!.filter { $0.hasSuffix(".jpg") || $0.hasSuffix("png") }
      let ir = Int(arc4random_uniform(UInt32(imagenames.count)))
      let imgurl = NSURL.fileURLWithPath(imagesDir + imagenames[ir])
    
      let workspace = NSWorkspace.sharedWorkspace()
      let screen = NSScreen.mainScreen()
      let ok : Bool = workspace.setDesktopImageURL( imgurl!, forScreen: screen!, options: nil, error: nil )
    
      if ok { 
        println( "New wallpaper: " + imagenames[ir] ) 
      } else { 
        println("Oops!")
      }
    }
    
    这在XCode 7 beta 3中不起作用

    我希望把重点放在以下几点:

    #!/usr/bin/env xcrun swift
    import Foundation
    import AppKit
    let imagesDir = "/Users/josh/Downloads/"
    let singleImage = "/Users/josh/Downloads/xlarge.png"
    
    
    let imgurl = NSURL.fileURLWithPath(singleImage)
    
    let workspace = NSWorkspace.sharedWorkspace()
    let screen = NSScreen.mainScreen()
    let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen!, options: nil, error: nil )
    
    if ok {
        print( "New wallpaper set!" )
    } else {
        print("Oops!")
    }
    
    并保存为文件
    wallper.swift

    执行时,错误为:

    ./wallpaper.swift:17:49: error: extra argument 'error' in call
    let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen!, options: nil, error: nil )
    
    现在我完全被卡住了。。。 我曾经尝试过参考和文档以及在操场上跑步,但这超出了我目前的技能

    删除它抱怨的额外参数(错误:nil)只会产生不同的错误:

    ./wallpaper.swift:13:31: error: cannot invoke 'setDesktopImageURL' with an argument list of type '(NSURL, forScreen: NSScreen?, options: nil)'
    let ok : Bool = workspace.setDesktopImageURL( imgurl, forScreen: screen, options: nil )
    

    代码在哪里失败,我如何理解如何使其正常工作?

    在您的示例中,您将
    nil
    作为
    选项传递给该方法

    我猜它以前是有效的,但现在在您显示当前方法签名的注释中:

    (url: NSURL, forScreen screen: NSScreen, options: [String : AnyObject]) throws
    
    我们看到,
    options
    应该是一个非可选字典

    这意味着对于
    options
    参数,您不能再使用
    nil
    :如果您没有选项,只需传递一个空字典即可

    另外,现在在Swift 2中,这个方法不再返回Bool,而是抛出

    这意味着您必须将其与
    一起使用,请尝试捕获

    do {
        let imgurl = NSURL.fileURLWithPath(singleImage)
        let workspace = NSWorkspace.sharedWorkspace()
        if let screen = NSScreen.mainScreen()  {
            try workspace.setDesktopImageURL(imgurl, forScreen: screen, options: [:])
        }
    } catch {
        print(error)
    }
    

    Swift 3的更新示例:

    do {
       let imgurl = NSURL.fileURL(withPath: singleImage)
       let workspace = NSWorkspace.shared()
       if let screen = NSScreen.main()  {
           try workspace.setDesktopImageURL(imgurl, for: screen, options: [:])
       }
    } catch {
       print(error)
    }
    

    对于我们这些使用Xamarin Mac的人,可以这样实现:

    string filepath=“/Users/you/Desktop/sweet wallpar.jpg”;
    var workspace=NSWorkspace.SharedWorkspace;
    var screen=NSScreen.MainScreen;
    NSUrl url=NSUrl.FromFilename(文件路径);
    NSDictionary选项=新建NSDictionary();
    NSError errorContainer=新的NSError();
    SetDesktopImageUrl(url,NSScreen.MainScreen,options,errorContainer);
    
    Swift 5.x的更新版本:

    do{
    让imageURL=URL(fileURLWithPath:“/path/to/image”)
    如果let screen=NSScreen.main{
    请尝试NSWorkspace.shared.setDesktopImageURL(imageURL,for:screen,options:[:])
    }
    }抓住{
    打印(错误)
    }
    
    不熟悉waith OSX函数,但从错误判断,您是否尝试过从“workspace.setDesktopImageURL”函数中删除“error:nil”?是的,返回的错误是
    /wallper3。swift:13:31:错误:无法使用类型为(NSURL,forScreen:NSScreen?,选项:nil)的参数列表调用“setDesktopImageURL”)'let ok:Bool=workspace.setDesktopImageURL(imgurl,forScreen:screen,options:nil)
    。“extra argument”错误似乎是“It's Breaked,figure It yourself”错误之一。在Xcode中,使用ALT+单击查看对象的类型。如果按ALT键并单击
    .setDesktopImageURL
    ,Xcode将显示此方法的类型和签名。然后您将看到必须使用的正确参数(文档可能未更新)。@EricD。很方便,谢谢。我点击了
    .setDesktopImageURL
    ,它说没有快速帮助,但当我点击命令时,它把我带到了声明(也很方便,不会想到这一点)。那里的代码是
    func setDesktopImageURL(url:NSURL,forScreen-screen:NSScreen,options:[String:AnyObject])抛出的
    。这对错误的位置更有意义吗?这段代码工作得很好,解释也很清楚。非常感谢。我还有很多东西要学。很有魅力!知道如何在多个空间上设置桌面图片吗?@Hexagons不幸的是没有API。有些人曾经工作过,但显然不再工作了。