Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# to Objective C by example #3
// This is the header file, "Person.h". The NSCopying is an example of implementing a protocol
@interface Person : NSObject //<NSCopying>;
{
// As with .NET, public for member variables isn`t recommended.
@public
BOOL thisIsWrong;
// Protected is the default for fields
@protected
Address *address;
NSString *title;
NSString *firstname;
NSString *lastname;
BOOL isMale;
// Self-explanatory
@private
// There's various ways of using double/floats.
// This is one of them, akin to a Javascript Number object (e.g. floats and ints).
NSNumber *age;
int ageInDogYears; // n.b. This is not a pointer
NSInteger *ageInCatYears;
// Some other C-based datatypes none of them used
char firstLetter;
double aDouble;
float aFloat;
// Collections - mutables allow you to add to them outside of the constructor.
NSArray *myArray;
NSMutableArray *myMutableArray;
NSMutableDictionary *myDictionary;
}
//
// Properties
//
// @property and @synthesis adds automatic getter and setters to the underlying fields.
// You list the fields you have above, and they are then accessible with dot notation
// with thread-safe getters/setters automatically created for you.
// The @synthesis is required in the @implementation. You access your properties using self-&gt;title
@property BOOL isMale;
@property NSString *title;
@property NSString *firstname;
@property NSString *lastname;
@property Address *address;
// Property options (more detail can be found at the links at the bottom of the page)
// Something a C# developer might find odd with objective-c is that if you subclass a class,
// you can make readonly or getter properties writable.
@property (readonly) BOOL isMale; // getter only
@property (readwrite) *firstname; // readwrite is the default and not required.
// Setter related options
@property (readonly,assign) NSString *title; // assign is the default and not required.
@property (retain) *lastname; // this indicates the property should be retained, incrementing the reference count
@property (copy) *address; // this indicates the object being assigned it copied, using its copy method.
//
// Methods -
// All methods are public, there is no access modifiers. The way to implement
// private methods is to remove the definition in the .h file, so they only
// exist in the implementation. Notice the location of the pointer '*' for definitions.
//
// A '+' indicates a static method
+(void) doVeryLittle;
+(BOOL) isLord : (NSString*) title;
+(NSString*) stringFormatExample : (NSString*) title : (NSString*) firstName;
// A '-' indicates instance methods
-(void) runForAnHour;
-(NSString*) getFullName;
-(NSString*) getNewTitle: (BOOL) isALord : (BOOL) isMale;
//
// Protocols
// Creating a protocol forces subclasses to implement the methods listed unless you mark them as optional.
// They are akin to interfaces however you can only force methods to be implemented. Classes can
// implement protocols from multiple different classes.
//
@protocol MyProtocol
- (int) setFavouriteBand;
@optional
-(int) isMadonnaFan;
@end
@end
//
// Categories
// This is simply a way of grouping methods together in smaller blocks, or categories.
// Other classes can also create new categories for your class, but they cannot add new fields.
//
@interface Person (Music) // The implementation code needs the brackets and category name too
-(void) setFavouriteBand;
-(void) isXFactorFan;
@end
// Implementation for the above
#import "Person.h"
@implementation Person
// To match the @property definitions in 'Person.h'
@synthesize isMale,title,firstname,lastname,address;
// You can also specify the backing field for a property (and override existing properties):
@synthesize title = internalTitle;
// Static variables (private)
// These are declared inside the implementation not the header.
static int serial;
@end
//
// Categories
// Implementing a category
//
@implementation Person (Music)
-(void) setFavouriteBand
{
}
-(void) isXFactorFan;
{
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.