AnyObject无法在Xcode8 beta6中运行? [英] AnyObject not working in Xcode8 beta6?

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

问题描述

在Xcode8 beta6中,以下代码将引发警告:'is'test始终为true。但它不会打印通行证。

  struct TestStruct {
}

//警告:'is'测试始终为true
如果TestStruct()是AnyObject {
print(pass)
}

以下代码会引发警告:从'T'到'AnyObject'的条件强制转换总是成功

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

//警告:从'T'到'AnyObject的条件强制转换'总是成功
守卫让对象=观察者为? AnyObject else {
fatalError(期望引用类型,但找到值类型:\(观察者))
}
// ... ...
}


解决方案

警告按预期工作: false 返回 TestStruct()是AnyObject ,但是,



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


'是'测试始终为真


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



鉴于对错误报告的评论由OP提交(SR-2420),情况似乎相反:从Xcode 8 / beta 6开始, test 总是评估为 true ,OP:s帖子的错误是 TestStruct()是AnyObject 在运行时评估为 false



Joe Groff写道:


这是正确的,因为一切都是桥梁现在 AnyObject



...



/ as AnyObject 现在总是对所有类型都成功。它按预期表现为







从Swift值转换为Obj-C对象的新 SwiftValue



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



好像未明确实现的Swift值似乎是可以通过例如桥接到Obj-C对象符合 _ObjectiveCBridgeable (有关<$ c的详细信息,请参阅以下Q& A) $ c> _ObjectiveCBridgeable ),会自动使用新的 SwiftValue 框来允许转换为Obj-C对象。 / p>

swift / stdlib / public / runtime / SwiftValue.mm 读取:


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



如果没有更好的Swift值映射到Objective-C
对于桥接目的,我们可以回退到
a类中的值。这个类没有任何公共接口,除了
NSObject - 在Objective-C中进行协调,但是被Swift
运行时识别,因此它可以动态地回送到盒装类型。



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")
    }

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)")
    }
    //...
}

解决方案

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' test is always true

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.

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.

Joe Groff writes:

This is correct, because everything bridges to AnyObject now.

...

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


The new SwiftValue box for conversion from Swift values to Obj-C objects

(for additional details, see discussion in the comments below, thanks @MartinR)

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.

The initial commit message for swift/stdlib/public/runtime/SwiftValue.mm reads:

Runtime: Implement an opaque 'SwiftValue' ObjC class to hold bridged values

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天全站免登陆