# Value type is AnyObject! Why?

**URL:** <https://forums.swift.org/t/value-type-is-anyobject-why/15290>\
**Category:** Using Swift\
**Created:** [August 16, 2018, 10:22am UTC](https://forums.swift.org/t/value-type-is-anyobject-why/15290 "2018-08-16T10:22:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vyacheslav](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Vyacheslav](https://forums.swift.org/u/Vyacheslav)\
**Post date:** [August 16, 2018, 10:22am UTC](https://forums.swift.org/t/value-type-is-anyobject-why/15290/1 "2018-08-16T10:22:58Z")

</div>

Can somebody explain this?

```
let array:[AnyObject] = []

if array is [Int] {
    print("// yes [AnyObject] is [Int]")
}

class Container<T> where T: AnyObject {
    init(value: T) {
        print("Initialize with object \(value)")
    }
}

```

and this

```
struct Cat {}

let cat = Cat()

class Container<T> where T: AnyObject {
    init(value: T) {
        print("Initialize with object \(value)")
    }
}
let container = Container(value: cat as AnyObject)

```

from here:

> **[Any\[Object\]](https://blog.krzyzanowskim.com/2018/05/29/any-object/)**
>
> You use Swift AnyObject wrong.
> 
> Don't use AnyObject as a generics constraint. Don't do "is AnyObject", "as? AnyObject" to check if a variable is a class type. Don't use "AnyObject". Use "class" for a class-only protocol. Use "Any".

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [August 16, 2018, 1:33pm UTC](https://forums.swift.org/t/value-type-is-anyobject-why/15290/2 "2018-08-16T13:33:47Z")

</div>

This is correct (albeit unintuitive at first) behaviour, and is a result of id-as-Any. See the following thread for more info:

> [@Any as AnyObject](https://forums.swift.org/t/any-as-anyobject/11182):
>
> I spotted a bug in a 3rd party library where a parameter type should be [String: Any]? instead of [String: AnyObject]?. While converting that dictionary I noticed that doing anyInstance as? AnyObject will raise a warning "Conditional cast from 'Any' to 'AnyObject' always succeeds". That behavior was unexpected since not every type is a reference type, hence not every any instance should be implicitly an any object instance. I know there are implicit wrapper types behind the scenes for Objective…

Regarding your example with the array:

```swift
let array: [AnyObject] = []

if array is [Int] {
    print("// yes [AnyObject] is [Int]")
}

```

it's worth noting that currently empty arrays can be cast to _any_ array type. For example:

```swift
let array: [String] = []

if array is [Int] {
  print("yup")
}
// prints: yup

```

There's some discussion of this behaviour in the comments of [SR-6192](https://bugs.swift.org/browse/SR-6192).

---

<div class="post-metadata">

**Author:** ![Vyacheslav](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Vyacheslav](https://forums.swift.org/u/Vyacheslav)\
**Post date:** [January 17, 2019, 9:59pm UTC](https://forums.swift.org/t/value-type-is-anyobject-why/15290/3 "2019-01-17T21:59:16Z")

</div>

Just funny compiler fact.

The compiler suggestion is totally incorrect:)

 ![19](https://global.discourse-cdn.com/swift/original/2X/8/8c20a296359bb6fd80a854bb62540857c222f0fa.png)

XCode Version 10.1 (10B61)
