Instruction
Structs & Classes
In the rest of this module, you’ll imagine that you’re creating a MetMuseum app to search for and display art objects by working with data from The Metropolitan Museum of Art Collection API, as if you were. Each art object has a huge number of properties, but your app will only need to use only a few of these.
Every art object has a unique objectID
. When your app searches for art objects that match a user query, you’ll first fetch an array of objectID
, then loop over this array to fetch each object’s data.
Your app will display the art object’s title
. As a courtesy to the museum, you should also display the object’s creditLine
— a description of how the museum acquired the object.
You’ll definitely want to show an image of the art object. The primaryImageSmall
property is the URL of the object’s image. For example, this is what you get when you load https://images.metmuseum.org/CRDImages/ep/web-large/DT1567.jpg
in a browser:
However, not all art objects have image URLs. Some art objects are not in the public domain, and their primaryImageSmall
value is an empty string. Your app must load the art object’s objectURL
to view its page at metmuseum.org. For example, https://www.metmuseum.org/art/collection/search/13061
:
Your app will model this data by defining a MuseumObject
type with these properties and use the isPublicDomain
property to implement a showImage
method.
Before diving into the demo, you should consider the nature of the data you’re working with and the intended behavior of instances. Structs are ideal for representing simple, lightweight values and data types, such as titles or coordinates. They are well-suited for instances that are frequently copied and where value semantics are important. On the other hand, classes are better suited for creating objects that require shared state or behavior across multiple instances. If you need to implement inheritance and create subclasses to extend functionality, classes provide the necessary tools. Additionally, classes are preferred when you require custom initialization or deinitialization logic. Now let’s define the art object in the demo.