为什么将结构强制转换为AnyObject而不是迅速的编译错误? [英] Why is casting a struct to AnyObject not a compile error in swift?

查看:51
本文介绍了为什么将结构强制转换为AnyObject而不是迅速的编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码可以快速编译和运行。

The code below compiles and works in swift.

struct TestStruct {
    let value: String = "asdf"
}

func iWantAReferenceType(object: AnyObject) {
    print(String(describing: object))
}

let o: TestStruct = TestStruct()

iWantAReferenceType(object: o as AnyObject)

我希望这是一个编译错误,因为结构永远不能符合AnyObject。如下面的无法编译的代码所示。

I expected this to be a compile error because a struct can never conform to AnyObject. As demonstrated below by code that fails to compile.

protocol Test: AnyObject {

}

//Compile error: because a struct cannot be AnyObject
struct TestStruct: Test {
    let value: String = "asdf"
}

我知道某些类型(例如String)可能会发生桥接。这将转换引用类型的值类型。

I am aware there is some bridging that can happen for certain types such as String. This would convert the value type of a reference type.

print(Mirror(reflecting: "asdf").subjectType) //print: String
print(Mirror(reflecting: "asdf" as AnyObject).subjectType) //print: NSTaggedPointerString

在写这个问题时,我想看看转换对象的类型是什么,似乎它也以某种方式被桥接。

In writing this question I thought to see what the type of the cast object was and it seems it is also bridged in someway.

print(Mirror(reflecting: o).subjectType) //prints: TestStruct
print(Mirror(reflecting: o as AnyObject).subjectType) //prints: _SwiftValue

为什么允许这种类型的转换?

Why is this type of casting allowed? It seems to be breaking the contract for the function that is expecting a reference type.

在重构一些支持值类型的代码时,我偶然发现了这个问题。尽管我认为不会,但它已经在为值类型工作。

I stumbled on this by accident when refactoring some code to support value types, to my surprise it had already been working for value types even though I thought it wouldn't. Is it safe to rely on this behaviour?

推荐答案

此功能是否便于传递给可可粉?任何结构都可以包装为 SwiftValue 引用类型。如果您打印 type(of:object),您将看到包装纸。

This is a feature to facilitate passing to Cocoa. Any struct can be wrapped into a SwiftValue reference type. If you print type(of: object) you'll see the wrapper.

我认为没有是预期参考类型的任何合同。更重要的是,尽管Swift中存在值类型和引用类型,但真正重要的是值和引用语义,它们在语言中无法表达。您可以在引用类型中创建值语义,而在值类型中创建引用语义,因此Swift类型系统在这方面确实没有任何帮助。

I don't think there is any contract for "expecting a reference type." More importantly, while "value types" and "reference types" exist in Swift, what really matter is value and reference semantics, which are not expressible in the language. You can create value semantics in reference types and reference semantics in value types, so the Swift type system really isn't of any help in that regard.

这里的重点是只有当您通过请求作为AnyObject 明确请求时,您才会得到这种异常行为。没有什么理由要写出来的,如果是的话,您最好确切地知道自己在做什么。

The important point here is that you only get this unusual behavior if you explicitly request it by asking for as AnyObject. There are very few reason to write that, and if you are, you had better know exactly what you're doing.

这篇关于为什么将结构强制转换为AnyObject而不是迅速的编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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