Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between NSObject and AnyObject? When to use the two?

Tags:

xcode

ios

swift

I'm trying to create an array to store Strings or an array of custom class. Am I better off creating a dictionary?

like image 631
Kim Montano Avatar asked Aug 28 '15 13:08

Kim Montano


1 Answers

AnyObject is Swift's representation of Objective-C's id type. It's more general than NSObject (i.e. every NSObject is an AnyObject, but not every AnyObject is a NSObject).

On the other part of the question - depends on how you intend to use the structure. Using a more particular class for the generic structure (Swift's typed array or dictionary) will allow you to enjoy the benefits of the strong typing, whereas using a more generic type will allow you to be more dynamic. Really it depends on what you intend to use the class for.

like image 195
Alexander Avatar answered Sep 18 '22 22:09

Alexander