Skip to content

Instantly share code, notes, and snippets.

@infinityrobot
Created August 10, 2012 06:07
Show Gist options
  • Save infinityrobot/3311622 to your computer and use it in GitHub Desktop.
Save infinityrobot/3311622 to your computer and use it in GitHub Desktop.
Obj-C @synthesize example
// I have included a few steps in here to show the flow I normally do to get things working.
// Keep in mind I'm still a massive noob so there may be a better way to do all of this :/
// ...
// Class definition and imports
// STEP 1: Declare vars in the controller @interface
@interface MapViewController : UIViewController <MKMapViewDelegate, UISearchBarDelegate> {
// ...
// Interface objects
MKMapView *mapView;
UISearchBar *searchBar;
// ...
// Result objects
NSMutableArray *results;
NSMutableData *requestData;
}
// STEP 2: Declare properties of all above declared vars
// Interface objects
@property (nonatomic, retain) MKMapView *mapView;
@property (nonatomic, retain) UISearchBar *searchBar;
// Result objects
@property (nonatomic, retain) NSMutableArray *results;
@property (nonatomic, retain) NSMutableData *requestData;
// ...
// Function definitions etc.
// Top of .m
// ...
// Imports
// Implementation
#pragma mark - MapViewController
@implementation MapViewController
// STEP 3: Synthesize the set ivars
@synthesize mapView, searchBar, results, requestData
// STEP 4: Now you should be able to use them :)
// NOTE: If you are using ARC - if you want to allocate a set variable to memory and persist it then do:
results = [NSMutableArray array]; // from memory?
// NOTE: If you are not using ARC:
results = [[NSMutableArray alloc] init]
// I only ever use @syntesize to connect the .h and .m - as I said I might be wrong but it has worked for me so far haha
// If anyone else wants to correct me somewhere please do :) I'm keen to learn!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment