user2722602
user2722602

Reputation:

AnyObject property in Swift

I have a question about property of AnyObject in Swift. I have some method with AnyObject variable. I can check what type is it, but for example I want to perform an operation on property regardless of the type of the variable (all possible types have the same property). Is that possible to call this varaiable?

Upvotes: 2

Views: 221

Answers (2)

Lucian Boboc
Lucian Boboc

Reputation: 490

AnyObject is used for compatibility with Cocoa and Objective C.

Upvotes: 3

Rob Napier
Rob Napier

Reputation: 299623

In almost all cases, an AnyObject property is a mistake. Almost always a protocol or generic was the right tool. For example, if you want to perform an operation "regardless of the type" then you really mean this type has some characteristic that would allow you to perform that operation. That characteristic (for instance, the existence of some property) is usually best expressed as a protocol.

Upvotes: 6

Related Questions