Since it doesn't say in the docs or anywhere online, how do you set objects in custom classes? For example, you make a custom class named Friends, and it has three columns: user (string), added (relation), friends (relation). How would you set user within your app, for example?
1 Answer
PFObject subclassing is in the documentation: https://www.parse.com/docs/ios_guide#subclasses/iOS
You will need to:
- Create a custom class (see example below)
- Initialize the class in your AppDelegate.m (see example below)
Example custom subclass for parse.com, with 'Organization' as the Parse class name:
Organization.h
//
// Organization.h
#import <Parse/Parse.h>
@interface Organization : PFObject<PFSubclassing>
+ (NSString *)parseClassName;
@property (retain) NSString *user;
@property (retain) NSString *addressLine1;
@property (retain) NSString *addressState;
@property (retain) NSString *addressZip;
@property (retain) NSString *phone;
@property (retain) NSString *email;
@property (retain) NSString *website;
@property (retain) NSString *contactFirstName;
@property (retain) NSString *contactLastName;
@property (retain) PFFile *logoImage;
@property (retain) NSString *orgDescription;
@property (retain) NSString *name;
@end
Organization.m
#import "Organization.h"
#import <Parse/PFObject+Subclass.h>
@implementation Organization
@dynamic addressCity;
@dynamic addressLine1;
@dynamic addressState;
@dynamic addressZip;
@dynamic phone;
@dynamic email;
@dynamic website;
@dynamic contactFirstName;
@dynamic contactLastName;
@dynamic logoImage;
@dynamic orgDescription;
@dynamic name;
+ (NSString *)parseClassName {
return @"Organization";
}
@end
How to initialize in the AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Organization registerSubclass]; //DO THIS BEFORE YOU START PARSE!
[Parse setApplicationId:YOUR_ID clientKey:YOUR_KEY];
....
8 Comments
smecperson
What if I already made the class in Parse dashboard? Also, instead of
PFObject, can my class be a subclass of UIViewController?Ryan Kreager
If you already made the class in the Parse dashboard, create your
PFObject subclass to match what you have in Parse. You have to do that anyways; a PFObject subclass will only work if you have the same object in the database. // You can't use` UIViewController` as the superclass for a Parse object class; you have to use PFObject.smecperson
So I have to make a new class in my xcode project, and I would have to make an instance to the class (Organization for example) in my view controller to access it?
Ryan Kreager
Correct - simply import the header of your
PFObject into your .m file for your view controller and go :)Ryan Kreager
+ (NSString *)parseClassName; is a special function used by Parse's SDK. No need to change that one, but do change the + (NSString *)parseClassName function in the .m file to return @"Friends"; |