加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – AnyObject在Xcode8 beta6中不工作?

发布时间:2020-12-15 01:57:22 所属栏目:百科 来源:网络整理
导读:在Xcode8 beta6中,以下代码将引发警告:’is’测试始终为真。但它不会打印通行证。 struct TestStruct { } //warning: 'is' test is always true if TestStruct() is AnyObject { print("pass") } 并且以下代码将导致警告:从“T”到“AnyObject”的条件转
在Xcode8 beta6中,以下代码将引发警告:’is’测试始终为真。但它不会打印通行证。

struct TestStruct {
    }

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

并且以下代码将导致警告:从“T”到“AnyObject”的条件转换总是成功

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,但是没有

这个答案的先前版本感觉到警告,

‘is’ test is always true

作为错误,并且包含一些关于为什么这种察觉到的错误警告将显示出来的讨论。 TestStruct()是AnyObject在运行时评估为false,但被认为是预期的行为。

考虑到bug report filed by the OP (SR-2420)的意见,似乎情况是相反的:由于Xcode 8 / beta 6,测试应该始终评估为true,而OP:s的错误是TestStruct()是AnyObject评估为运行时错误

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.

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

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

看起来好像没有明确实现的Swift值可以通过例如OBJ-C对象来桥接。与_ObjectiveCBridgeable(参见例如the following Q&A for details regarding _ObjectiveCBridgeable)一致,将自动使用新的SwiftValue框来允许转换到Obj-C对象。

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

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读