//The class declaration always begins with the @interface compiler directive and ends with the @end directive.
//MyClass.h
@interface MyClass : NSObject
{
int count;
NSString* name;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int count;
-(MyClass*)thisIsAMethod:(NSString*)aName;
@end
//MyClass.m
@implementation MyClass
@synthesize name;
@synthesize count;
+ (void) initialize {
self.name=[NSString stringWithFormat:@"Something"];
}
-(MyClass*)thisIsAMethod:(NSString*)aName {
NSLog(aName);
return self;
}
@end
//** Declared Properties **
@property BOOL flag;
@property (copy) NSString *nameObject; // Copy the object during assignment.
@property (readonly) UIView *rootView; // Declare only a getter method.
@property (retain) NSString *name; //Specifies that retain should be invoked on the object upon assignment.
@property (nonatomic) NSArray *array; //Specifies that accessors are non-atomic. By default, accessors are atomic.
//You can combine the @property in a single line if you want:
@property (nonatomic, assign) int number1, number2, number3;
//Note: Each readable property specifies a method with the same name as the property. Each writable property specifies an additional method of the form setPropertyName:, where the first letter of the property name is capitalized.
//In your class implementation, you can use the @synthesize compiler directive to ask the compiler to generate the methods according to the specification in the declaration:
@synthesize flag;
@synthesize nameObject;
@synthesize rootView;
//You can combine the @synthesize statements in a single line if you want:
@synthesize flag, nameObject, rootView;
// UIAction and IBOutlet
// IBAction and IBOutlet are macros defined to point variables and methods to the Interface Builder.
@interface FirstView : UIViewController {
IBOutlet UITextField *personFilter; //this object will be able to point to a Text Field Input in the Interface
//[...]
}
-(IBAction) touchSearch:(id)sender; //this method will be able to point to an event in the Interface
@end
//** Methods and Messaging Syntax **
//Method declaration syntax:
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
// - is method type identifier
// (void) is Return type
// insertObject, atIndex: Method signature keyworks
// id, NSUInteger: Parameter types
// anObject, index: Parameter names
//Example
-(NSString *) getFilterName:(SearchFilters) filter
//Example
- (NSString *) getName:(NSString *)thisArray
atIndex:(int)index
//** Messaging **
//When you want to call a method, you do so by messaging an object.
//Messages are enclosed by brackets ([ and ]). Inside the brackets, the object receiving the message is on the left side and the message (along with any parameters required by the message) is on the right.
[myArray insertObject:anObject atIndex:0];
//To avoid declaring numerous local variables to store temporary results:
[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0];
//Objective-C also provides a dot syntax for invoking accessor methods:
[myAppObject.theArray insertObject:[myAppObject objectToInsert] atIndex:0];
//You can also use dot syntax for assignment:
myAppObject.theArray = aNewArray;
//The syntax for a class method declaration is identical to that of an instance method, with one exception. Instead of using a minus sign for the method type identifier, you use a plus (+) sign.
//** Constructor **
+ (void) initialize {
// Initialization for this class and any subclasses
}
//Strongly and weakly typed variable declarations:
//When storing objects in variables, you always use a pointer type. Objective-C supports both strong and weak typing for variables containing objects. Strongly typed pointers include the class name in the variable type declaration. Weakly typed pointers are used frequently for things such as collection classes, where the exact type of the objects in a collection may be unknown.
MyClass *myObject1; // Strong typing
id myObject2; // Weak typing
More info: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/
Tags: @property, @synthesize, class, IBAction, IBOutlet, implementation, interface