小编MAH*_*MAH的帖子

我的类可以覆盖Swift中的协议属性类型吗?

protocol Parent {
     var children: [AnyObject] { get set }
}

class Foo {
}

class Bar: Parent { //error happens here
    var children = [Foo]()

    init() {}
}
Run Code Online (Sandbox Code Playgroud)

我得到一个错误"类型'对象'不符合协议'父'.我得到这个错误的原因是因为我将子节点定义为Foo而不是AnyObject的数组.有什么方法可以使这个工作?

protocols swift

8
推荐指数
1
解决办法
3554
查看次数

在 Vapor 4 中的文件流之后运行操作

我运行一个处理大文件(上传和下载)的网站。目前我正在将网站从 Perfect 过渡到 Vapor。在 Perfect 中,将文件流式传输给用户,然后在文件完成后执行一些操作相对简单。

我试图在 Vapor 中做同样的事情,但我似乎无法弄清楚如何在流完成时设置回调,以及当它完成时,它是否完全被用户下载或有中断。

有谁知道如何做到这一点?这是我尝试过的一些事情。

这是基本结构

func downloadFile(request: Request) -> EventLoopFuture<Response> {
    //do some authentication stuff here
    
    let promise = request.eventLoop.makePromise(of: Response.self)
    let response = request.fileio.streamFile(at: "somePath")
    promise.succeed(response)
    let future = promise.futureResult
    return future
}
Run Code Online (Sandbox Code Playgroud)

第一次修改

func downloadFile(request: Request) -> EventLoopFuture<Response> {
    //do some authentication stuff here
    
    let promise = request.eventLoop.makePromise(of: Response.self)
    let response = request.fileio.streamFile(at: "somePath")
    promise.succeed(response)
    let future = promise.futureResult
    
    future.eventLoop.next().execute {
        //run some post-download processing here.
        //ideally I would like to know …
Run Code Online (Sandbox Code Playgroud)

swift vapor server-side-swift swift-nio

5
推荐指数
1
解决办法
118
查看次数

动态改变NSWindow的isMovableByWindowBackground和isMovable

我一直在努力解决这个非常令人沮丧的问题,希望有人能提供帮助。我正在尝试创建一个具有多个视图的 NSWindow。视图可以相互重叠。这些就是要求。

  • 如下图所示,蓝色的 NSView 是可以移动窗口的。
  • 黄色的 NSView 无法移动窗口。这是另一个 NSView 实例,位于蓝色 NSView 下方。
  • 该运动需要感觉像任何其他窗口运动一样。
  • 将窗口一直拖到顶部之类的操作应该激活 Expose(Sierra 和 High Sierra)。

在此输入图像描述

第一次尝试

我已经通过尝试以下方法进行了尝试。不幸的是,这根本行不通。

在 NSWindow 中

override func awakeFromNib() {
    self.refreshElements()
    self.titlebarAppearsTransparent = true
    self.isMovableByWindowBackground = true
}
Run Code Online (Sandbox Code Playgroud)

在我的自定义视图中

class Blue: NSView {
    public override var mouseDownCanMoveWindow: Bool { return true }
}

class Yellow: NSView {
    public override var mouseDownCanMoveWindow: Bool { return false }
}
Run Code Online (Sandbox Code Playgroud)

第二次尝试

然后我尝试在 Blue 中实现mouseDown mouseDragged和。mouseUp我会计算鼠标self.setFrame(frameRect:display:animate:)在窗口上的移动。这很有效,因为我可以控制移动窗口,但它有几个问题。无论如何,窗口的移动速度总是较慢。我认为即使我将animate属性设置为 false,它也会有动画。而且它不像其他窗口那样激活 Expose。


第三次尝试 …

cocoa

1
推荐指数
1
解决办法
621
查看次数

标签 统计

swift ×2

cocoa ×1

protocols ×1

server-side-swift ×1

swift-nio ×1

vapor ×1