iOS engineer who likes clean, simple designs and sans-serif.

Loading

Creating PFObjects With Parse

Here’s a basic example of how to model objects in Parse with PFObjects. Long term though, it may be better to use the API instead in case you decide you need to migrate off Parse to your own homegrown backend.

Quote.h

#import <Parse/Parse.h>
@interface LCLQuote : PFObject <PFSubclassing>

@property (strong, nonatomic) NSString *quote;
@property (strong, nonatomic) NSString *author;

+ (NSString *)parseClassName;
+ (instancetype)quoteWithText:(NSString *)quoteString WithAuthor:(NSString *)authorString;
@end

Quote.m

#import "LCLQuote.h"
#import <Parse/PFObject+Subclass.h>

@implementation LCLQuote
@dynamic quote;
@dynamic author;

# pragma mark - Setters and Getters
+ (NSString *)parseClassName {
    return @"LCLQuote";
}

# pragma mark - Init Methods
+ (instancetype)quoteWithText:(NSString *)quoteString WithAuthor:(NSString *)authorString
{
    return [[self alloc] initWithClassName:[LCLQuote parseClassName] WithText:quoteString WithAuthor:authorString];
}

- (instancetype)initWithClassName:(NSString *)newClassName WithText:(NSString *)quoteString WithAuthor:(NSString *)authorString]
{
    self = [super init];
    if (self) {
        self.quote = quoteString;
        self.author = authorString;
        [self saveInBackground];
    }
    return self;
}

Initialization

LCLUser *joe = [LCLUser userWithName:@"Joe" WithRole:@"Lead iOS Instructor"];
LCLQuote *joeQuote1 = [LCLQuote quoteWithText:@"Sweet" WithAuthor:@"Joe"];
LCLQuote *joeQuote2 = [LCLQuote quoteWithText:@"That’s all I got" WithAuthor:@"Joe"];
LCLQuote *joeQuote3 = [LCLQuote quoteWithText:@"It’s crazy" WithAuthor:@"Joe"];
LCLQuote *joeQuote4 = [LCLQuote quoteWithText:@"Go team" WithAuthor:@"Joe"];

LCLUser *al = [LCLUser userWithName:@"Al" WithRole:@"Chief iOS TA"];
LCLQuote *alQuote1 = [LCLQuote quoteWithText:@"Ahhh" WithAuthor:@"Al"];
LCLQuote *alQuote2 = [LCLQuote quoteWithText:@"I love core data" WithAuthor:@"Al"];
LCLQuote *alQuote3 = [LCLQuote quoteWithText:@"I’ll talk to Joe about it" WithAuthor:@"Al"];

Leave a Comment