Incorporating the Data
After you implement your app’s behavior, you create a data model to support your app’s interface. An app’s data model defines the way you maintain data in your app. Data models can range from a basic dictionary of objects to complex databases. A good data model makes it easier to build a scalable app, improve functionality, and make changes to your features.
Your app’s data model is composed of your data structures and (optionally) custom business logic needed to keep that data in a consistent state. You never want to design your data model in total isolation from your app’s user interface. You do, however, want to implement your data model objects separately, without relying on the presence of specific views or view controllers. When you keep your data separate from your user interface, you’ll find it easier to implement a universal app—one that can run on both iPad and iPhone—and easier to reuse portions of your code later.
Designing Your Model
If you simply need to store a small amount of data, Foundation framework classes may be your best option. Research existing Foundation classes to see what behaviors are available for you to use instead of attempting to implement the same thing on your own. For example, if your app only needs to keep track of a list of strings, you can rely on
NSString to do the job for you. You’ll learn more about these and other Foundation classes in Working with Foundation.
If your data model requires custom business logic in addition to just storing data, you can write a custom class. Consider how you can incorporate existing framework classes into the implementation of your own classes. It’s beneficial to use existing framework classes within your custom classes instead of trying to reinvent them. For example, a custom class might use
NSMutableArray to store information—but define its own features for working with that information.
When you design your data model, ask yourself these questions:
How will the user use your app, and what types of data do you need to store? Make sure your model reflects the app’s content and purpose. Even though the user doesn’t interact with the data directly, there should be a clear correlation between the interface and the data. Whether you’re storing text, documents, large images, or another type of information, design your data model to handle that particular type of content appropriately.
What data structures can you use? Determine where you can use framework classes and where you need to define classes with custom functionality.
How will you supply data to the user interface? Your model shouldn’t communicate directly with your interface. To handle the interaction between the model and the interface, you’ll need to add logic to your controllers.
Implementing Your Model
In Tutorial: Storyboards, you started working with the Objective-C programming language. Although this guide teaches you how to build a simple app, you’ll want to become familiar with the language before writing your own fully functional app.
Some people learn the concepts by reading Programming with Objective-C and then writing a number of small test apps to solidify their understanding of the language and to practice writing good code. Others jump right into programming and look for more information when they don’t know how to accomplish something. If you prefer this approach, keep Programming with Objective-C as a reference and make it an exercise to learn concepts and apply them to your app as you develop it.
The most important goal in developing your first data model is to get something that works. Think carefully about the structure of your data model, but don’t worry about making it perfect. Don’t be afraid to iterate and refine your model after you begin implementing it.
Incorporating Real Data
When you first test your data model, you may want to use static or fake data until you know the model is assembled and connected properly. After you’ve defined a working data model, you can pull real data into your app.