是Float,Double,Int是AnyObject吗?

时间:2022-08-02 16:06:18

I read inline documentation of Swift and I am bit confused.

我阅读了Swift的内联文档,我有点困惑。

1) Any is a protocol that all types implicitly conform.

1)任何类型都是隐式符合的协议。

2) AnyObject is a protocol to which all classes implicitly conform.

2)AnyObject是所有类隐式符合的协议。

3) Int, Float, Double are structs

3)Int,Float,Double是结构

Here is a sample code:

这是一个示例代码:

import UIKit

func passAnyObject(param: AnyObject) {
    print(param)
}

class MyClass {}
struct MyStruct {}

let a: Int = 1
let b = 2.0
let c = NSObject()
let d = MyClass()
let e = MyStruct()

passAnyObject(a)
passAnyObject(b)
passAnyObject(c)
passAnyObject(d)
//passAnyObject(e) // Argument type 'MyStruct' does not conform to expected type 'AnyObject'


if a is AnyObject { // b, d, e is also AnyObject
    print("\(a.dynamicType) is AnyObject")
}

What I don't understand is why Int, Double, Float are AnyObjects? Why compiler doesn't say anything? Those types are declared as structs. Struct MyStruct cannot be passed to the method on the top because it does not conform to AnyObject.

我不明白为什么Int,Double,Float是AnyObjects?为什么编译器什么都没说?这些类型被声明为结构。 Struct MyStruct无法传递给顶部的方法,因为它不符合AnyObject。

Could you help me understand why Int, Double and Float are AnyObject or why compiler thinks they are?

你能帮我理解为什么Int,Double和Float是AnyObject或为什么编译器认为它们是?

2 个解决方案

#1


30  

Because you have Foundation imported, Int, Double, and Float get converted to NSNumber when passed to a function taking an AnyObject. Type String gets converted to NSString. This is done to make life easier when calling Cocoa and Cocoa Touch based interfaces. If you remove import UIKit (or import Cocoa for OS X), you will see:

因为您已导入Foundation,所以Int,Double和Float在传递给采用AnyObject的函数时会转换为NSNumber。 Type String转换为NSString。这样做是为了在调用基于Cocoa和Cocoa Touch的界面时使生活更轻松。如果删除导入UIKit(或导入OS X的Cocoa),您将看到:

error: argument type 'Int' does not conform to expected type 'AnyObject'

when you call

你打电话时

passAnyObject(a)

This implicit conversion of value types to objects is described here.

这里描述了值类型到对象的隐式转换。


Update for Swift 3 (Xcode 8 beta 6):

Passing an Int, Double, String, or Bool to a parameter of type AnyObject now results in an error such as Argument of type 'Int' does not conform to expected type 'AnyObject'.

将Int,Double,String或Bool传递给AnyObject类型的参数现在会导致错误,例如“Int”类型的Argument不符合预期类型“AnyObject”。

With Swift 3, implicit type conversion has been removed. It is now necessary to cast Int, Double, String and Bool with as AnyObject in order to pass it to a parameter of type AnyObject:

使用Swift 3,隐式类型转换已被删除。现在需要使用AnyObject转换Int,Double,String和Bool,以便将其传递给AnyObject类型的参数:

let a = 1
passAnyObject(a as AnyObject)

#2


1  

Good find! UIKit actually converts them to NSNumber - also mentioned by @vacawama. The reason for this is, sometimes you're working with code that returns or uses AnyObject, this object could then be cast (as!) as an Int or other "structs".

很好找! UIKit实际上将它们转换为NSNumber - @vacawama也提到过。原因是,有时你正在处理返回或使用AnyObject的代码,然后可以将此对象作为Int或其他“结构”进行转换(如!)。

#1


30  

Because you have Foundation imported, Int, Double, and Float get converted to NSNumber when passed to a function taking an AnyObject. Type String gets converted to NSString. This is done to make life easier when calling Cocoa and Cocoa Touch based interfaces. If you remove import UIKit (or import Cocoa for OS X), you will see:

因为您已导入Foundation,所以Int,Double和Float在传递给采用AnyObject的函数时会转换为NSNumber。 Type String转换为NSString。这样做是为了在调用基于Cocoa和Cocoa Touch的界面时使生活更轻松。如果删除导入UIKit(或导入OS X的Cocoa),您将看到:

error: argument type 'Int' does not conform to expected type 'AnyObject'

when you call

你打电话时

passAnyObject(a)

This implicit conversion of value types to objects is described here.

这里描述了值类型到对象的隐式转换。


Update for Swift 3 (Xcode 8 beta 6):

Passing an Int, Double, String, or Bool to a parameter of type AnyObject now results in an error such as Argument of type 'Int' does not conform to expected type 'AnyObject'.

将Int,Double,String或Bool传递给AnyObject类型的参数现在会导致错误,例如“Int”类型的Argument不符合预期类型“AnyObject”。

With Swift 3, implicit type conversion has been removed. It is now necessary to cast Int, Double, String and Bool with as AnyObject in order to pass it to a parameter of type AnyObject:

使用Swift 3,隐式类型转换已被删除。现在需要使用AnyObject转换Int,Double,String和Bool,以便将其传递给AnyObject类型的参数:

let a = 1
passAnyObject(a as AnyObject)

#2


1  

Good find! UIKit actually converts them to NSNumber - also mentioned by @vacawama. The reason for this is, sometimes you're working with code that returns or uses AnyObject, this object could then be cast (as!) as an Int or other "structs".

很好找! UIKit实际上将它们转换为NSNumber - @vacawama也提到过。原因是,有时你正在处理返回或使用AnyObject的代码,然后可以将此对象作为Int或其他“结构”进行转换(如!)。