热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GKAgentDelegate的协议方法不正确吗?-TheprotocolmethodofGKAgentDelegateisnotrightinSwift?

InGameplayKit,IwanttoconformtheprotocolGKAgentDelegate,henceusethedelegatemethodfunca

In GameplayKit, I want to conform the protocol GKAgentDelegate, hence use the delegate method func agentDidUpdate(agent: GKAgent).

在GameplayKit中,我希望遵守协议GKAgentDelegate,因此使用委托方法func agentDidUpdate(agent: GKAgent)。

The problem is, in this method, the parameter agent is declared as GKAgent, not GKAgent2D, so that I cannot access agent.position, because the position property is in GKAgent2D, not GKAgent...

问题是,在这个方法中,参数代理被声明为GKAgent,而不是GKAgent2D,因此我无法访问代理。位置,因为位置属性在GKAgent2D中,而不是GKAgent…

However in Objective-C API, agent is declared as GKAgent2D.

但是在Objective-C API中,代理被声明为GKAgent2D。

Please help.

请帮助。

1 个解决方案

#1


2  

As GKAgent2D is a subclass of GKAgent, you can simply upcast it:

由于GKAgent2D是GKAgent的一个子类,您可以简单地将其上传:

func agentDidUpdate(agent: GKAgent) {
    if let agent2d = agent as? GKAgent2D {
        // do thing with agent2d.position
    }
}

The documentation for GKAgentDelegate (from Xcode 7, beta 1; i.e. the OSX 10.11 SDK) shows the same type (GKAgent) for both languages:

GKAgentDelegate的文档(来自Xcode 7, beta 1;即osx10.11 SDK)显示了两种语言相同的类型(GKAgent):

enter image description here

So it should have always been GKAgent under Objective-C in order to be strictly correct, however Objective-C is less type-safe than Swift, so you could get away with it (with the possibility that you'd get an unrecognized selector exception if a GKAgent object was ever passed to the delegate method and you assumed it was a GKAgent2D).

所以它应该在objective - c一直GKAgent为了严格正确,然而objective - c是不如迅速类型安全的,所以你可以侥幸成功(可能你会得到一个未被选择器异常如果GKAgent对象被传递给委托方法,你认为这是一个GKAgent2D)。


推荐阅读
author-avatar
小爬虫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有