iOS Tutorial iOS Tutorial Part I

View the Project on GitHub FatFractal/hoodyoodoo

Add the Celebrity Model

In this section, we will create the Objective-C native, object classes that the app will use. At this point, we will create a Celebrity object using standard Objective-C patterns. This is your object definition, the NoServer Fraamework does not create dependencies in your code by requiring you to use our own proprietary object classes.

Creating the Celebrity Class

We will create a Celebrity data model that contains the following members:

Celebrity
{string} firstName the celebrity’s first name
{string} lastName the celebrity’s last name
{byte[]} imageData  the celebrity’s profile image
{string} gender the gender of the celebrity

You may notice that we are anticipating a byte array datatype for the image data as a native member of this class–stay tuned to see how that works.

In order to create the Celebrity class for your project, choose File>New>New File to bring up the Template Chooser for the new class file. We will use the standard Objective-C class template. Click Next.

Enter the name of the Objective-C class, Celebrity, as a subclass of NSObject.

Click Next to create a Celebrity.h and Celebrity.m files in your project, which contain the following information, respectively:


#import <Foundation/Foundation.h>
@interface Celebrity : NSObject
@end

#import "Celebrity.h"
@implementation Celebrity
@end

Modify the files to include the members for the object. For Celebrity class, we will modify Celebrity.h.

@interface Celebrity : NSObject {
    /*! The NSString that contains the First Name for this Celebrity */
    NSString        *firstName;
    /*! The NSString that contains the Last Name for this Celebrity */
    NSString        *lastName;
    /*! The NSData for the profile image for this Celebrity */
    NSData          *imageData;
    /*! The NSString that contains the gender (male or female) for this Celebrity */
    NSString        *gender;
}
@property (strong, nonatomic) NSString          *firstName;
@property (strong, nonatomic) NSString          *lastName;
@property (strong, nonatomic) NSData            *imageData;
@property (strong, nonatomic) NSString          *gender;

@end

For Celebrity.m, we use a standard model approach and include a description method (always a good practice).

#import "Celebrity.h"
@implementation Celebrity
@synthesize firstName, lastName, imageData, gender;
- (id)init {
    self = [super init];
    return self;
}
- (NSString*) description {
    return [[NSString alloc]
    initWithFormat:@"Celebrity[firstName[%@], lastName[%@], imageData[%d], gender[%@]]",
    [self firstName],
    [self lastName],
    [[self imageData] length],
    [self gender]];
}
@end

The Celebrity object is now ready to interact with your API.

NEXT: Add in the Class