Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

preface

Tomorrow is the National Day 7-day holiday, today we talk about some easy entry.

How to understand Any and Any object in Swift? This for a transition from OC to Swift, or just entering Swift children’s shoes will have a certain small vexation?

Here is my understanding.

There are three categories in Swift

This is a good place to note that Swift data is currently defined by three broad categories:

  • Struct (structure)

  • Enum (enum)

  • Class (class)

Struct and enum can be said to be the same root, both on the stack, the program monkey does not have to manage its life cycle.

Class on the heap, the programmer needs to manage its life cycle.

Again, structs and enums on the stack are value types, and classes on the heap are reference types.

Don’t understand first write down, these are need actual combat to fully understand.

Any

Let’s look at what Any is:

protocol Any {}
Copy the code

Any is an empty protocol, and according to the literature, the three classes implicitly comply with this protocol when they are created. You can interpret this as:


struct Person: Any {}

class Animal: Any {}

enum Type: Any {}

Copy the code

So to speak, Any can represent Any type of Swift language build.

Add: include function types, i.e., closure types. like() -> VoidYou can also use Any

AnyObject

In fact, AnyObject is relatively simple. It represents the type of the class. I have selected a part of the source code and comments, you will understand.

/// The protocol to which all classes implicitly conform.
///
/// You use `AnyObject` when you need the flexibility of an untyped object or
/// when you use bridged Objective-C methods and properties that return an
/// untyped result. `AnyObject` can be used as the concrete type for an
/// instance of any class, class type, or class-only protocol.

public typealias AnyObject
Copy the code

So, you might be wondering, we often write code like this:

class AClass: NSObject {}
Copy the code

What is the relationship between AnyObject and NSObject?

There are actually two things I want to explain:

  • Instead of inheriting NSObject when you write a class, the class in Swift implicitly inherits a particular swift.swiftobject. It is recommended that you add NSObject when the IDE prompts you to do so, usually when you need to implement the proxy.

  • AnyObject can represent class, and class: NSObject is in any case just a more refined branch of class, so it makes sense that AnyObject contains NSObject.

Reference Documents:

SwiftObject.h

conclusion

Any can represent Any type, including function types, and the following code works fine:

let callback: (() -> Void) = {  }

func aFunction(arg: Any) {

}

aFunction(arg: callback)
Copy the code

AnyObject represents an object instance of any class type, and the class that inherits NSObject is just a subset of AnyObject.

We’ll see you next time.