AnyObject 在 Xcode8 beta6 中不起作用? [英] AnyObject not working in Xcode8 beta6?

查看:24
本文介绍了AnyObject 在 Xcode8 beta6 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xcode8 beta6 中,以下代码会导致警告:'is' test always true.但它不会打印pass.

In Xcode8 beta6, the following code will cause a warning: 'is' test is always true. But it won't print pass.

    struct TestStruct {
    }

    //warning: 'is' test is always true
    if TestStruct() is AnyObject {
        print("pass")
    }

并且下面的代码会引起警告:从'T'到'AnyObject'的条件转换总是成功

And the following code will cause a warning: Conditional cast from 'T' to 'AnyObject' always succeeds

public static func register<T>(_ protocolType: T.Type, observer: T) {

    //Warning: Conditional cast from 'T' to 'AnyObject' always succeeds
    guard let object = observer as? AnyObject else {
        fatalError("expecting reference type but found value type: (observer)")
    }
    //...
}

推荐答案

警告按预期工作:TestStruct() 的 false 返回值为 AnyObject,但是,不会

The warning works as intended: the false return of TestStruct() is AnyObject, however, does not

此答案的先前版本感知到警告,

The prior version of this answer perceived the warning,

'is' 测试总是正确的

作为错误,并包含一些关于为什么会出现这种感知错误警告的讨论.TestStruct() is AnyObject 在运行时被评估为 false,然而,被认为是预期的行为.

as the bug, and contained some discussion as to why this perceived buggy warning would manifest itself. That TestStruct() is AnyObject evaluated to false at runtime, however, was perceived as expected behaviour.

鉴于对 OP (SR-2420) 提交的错误报告的评论,似乎情况正好相反:由于 Xcode 8/beta 6,is 测试 应该 总是评估为 true,并且错误OP:的帖子是 TestStruct() is AnyObject 在运行时评估为 false 的事实.

Given the comments to the bug report filed by the OP (SR-2420), it seems the situation is the reverse: since Xcode 8/beta 6, the is test should always evaluate to true, and the bug the OP:s post is the fact that TestStruct() is AnyObject evaluates to false during runtime.

乔·格罗夫写道:

这是正确的,因为现在一切都连接到 AnyObject.

This is correct, because everything bridges to AnyObject now.

...

is/as AnyObject 现在对于所有类型总是成功.它的行为如预期的那样.

is/as AnyObject always succeed for all types now. It's behaving as intended.

<小时>

用于将 Swift 值转换为 Obj-C 对象的新 SwiftValue

(有关更多详细信息,请参阅下面评论中的讨论,感谢@MartinR)

似乎没有明确实现的 Swift 值可以通过例如桥接到 Obj-C 对象_ObjectiveCBridgeable 的一致性(参见例如 以下问答,了解有关 _ObjectiveCBridgeable),而是会自动使用新的 SwiftValue 框来允许转换为 Obj-C 对象.

It seems as if Swift values that are not explicitly implemented to be bridgeable to Obj-C objects via e.g. conformance to _ObjectiveCBridgeable (see e.g. the following Q&A for details regarding _ObjectiveCBridgeable), will instead automatically make use of the new SwiftValue box to allow conversion to Obj-C objects.

swift/stdlib/的初始提交消息public/runtime/SwiftValue.mm 读作:

运行时:实现一个不透明的SwiftValue"ObjC 类来保存桥接值

如果没有更好的将 Swift 值映射到 Objective-C 的方法用于桥接目的的对象,我们可以回退到装箱中的值一类.这个类没有任何公共接口,除了NSObject - 符合 Objective-C,但被 Swift 识别运行时,以便它可以动态转换回装箱类型.

If there's no better mapping for a Swift value into an Objective-C object for bridging purposes, we can fall back to boxing the value in a class. This class doesn't have any public interface beyond being NSObject-conforming in Objective-C, but is recognized by the Swift runtime so that it can be dynamically cast back to the boxed type.

这篇关于AnyObject 在 Xcode8 beta6 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆