id
id anObject;
This type is defined as a pointer to an object—in reality,
nil
NULL
[…]
object messaging
[receiver message]
[myRect display];
[myRect setWidth:20.0];
[myRect setOrigin:30.0 :50.0]; // This is a bad example of multiple arguments
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
BOOL isFilled = [myRect isFilled]; // if myRect filled returns YES,
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
[myRect setPrimaryColor:[otherRect primaryColor]];
.
Dot Syntax
myInstance.value = @”New value”; //[myInstance setValue:@”New value”];
NSLog(@”myInstance value: %@”, myInstance.value); //NSLog(@”myInstance value: %@”, [myInstance value]);
string
NSString* myString = @"My String\n"; NSString* anotherString = [NSString stringWithFormat:@"%d %s", 1, @"String"]; // Create an Objective-C string from a C string NSString* fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];
Listing 1-1 Accessing properties using the dot syntax
Graphic *graphic = [[Graphic alloc] init];
NSColor *color = graphic.color;
CGFloat xLoc = graphic.xLoc;
BOOL hidden = graphic.hidden;
int textCharacterLength = graphic.text.length;
if (graphic.textHidden != YES) {
graphic.text = @”Hello”;
}
graphic.bounds = NSMakeRect(10.0, 10.0, 20.0, 120.0)
Listing 1-2 Accessing properties using accessor methods, as same as Listing 1-1
Graphic *graphic = [[Graphic alloc] init];
NSColor *color = [graphic color];
CGFloat xLoc = [graphic xLoc];
BOOL hidden = [graphic hidden];
int textCharacterLength = [[graphic text] length];
if ([graphic isTextHidden] != YES) {
[graphic setText:@”Hello”];
}
[graphic setBounds:NSMakeRect(10.0, 10.0, 20.0, 120.0)];
Listing 1-3
NSMutableData *data = [NSMutableData dataWithLength:1024];
data.length += 1024; //[data setLength:[data length] + 1024];
data.length *= 2; //[data setLength:[data length] * 2];
data.length /= 4; //[data setLength:[data length] / 4];
Listing 1-4
// each member of the path is an object
x = person.address.street.name; // x = [[[person address] street] name];
// the path contains a C struct
// will crash if window is nil or -contentView returns nil
y = window.contentView.bounds.origin.y; // y = [[window contentView] bounds].origin.y;
// an example of using a setter….
person.address.street.name = @”Oxford Road”; // [[[person address] street] setName: @”Oxford Road”];
Self
self.age = 10;
Key-value coding (KVC)
Sample: define a class
@interface MyClass
@property NSString *stringProperty;
@property NSInteger integerProperty;
@property MyClass *linkedInstance;
@end
Used the class in below, sample 1
MyClass *myInstance = [[MyClass alloc] init];
NSString *string = [myInstance valueForKey:@”stringProperty”];
[myInstance setValue:[NSNumber numberWithInt:2] forKey:@”integerProperty”];
sample2
MyClass *anotherInstance = [[MyClass alloc] init];
myInstance.linkedInstance = anotherInstance;
myInstance.linkedInstance.integerProperty = 2;
sample3: This has the same result as:
MyClass *anotherInstance = [[MyClass alloc] init];
myInstance.linkedInstance = anotherInstance;
[myInstance setValue:[NSNumber numberWithInt:2] forKeyPath:@”linkedInstance.integerProperty”];
aVariable = anObject.aProperty;
anObject.name = @”New Name”;
xOrigin = aView.bounds.origin.x;
NSInteger i = 10;
anObject.integerProperty = anotherObject.floatProperty = ++i;
Class
Rectangle *myRect;
Graphic *myRect;
if ( [anObject isMemberOfClass:someClass] )
if ( [anObject isKindOfClass:someClass] )
Class Objects
int versionNumber = [Rectangle version];
id aClass = [anObject class];
id rectClass = [Rectangle class];
Class aClass = [anObject class];
Class rectClass = [Rectangle class];
Creating Instances
id myRect;
myRect = [Rectangle alloc]; // dynamically allocates memor
myRect = [[Rectangle alloc] init]; //same as, myRect = [Rectangle alloc];
[myMatrix setCellClass:[NSButtonCell class]];
Class Object
create Class Object
int MCLSGlobalVariable;
@implementation MyClass
// implementation continues
static MyClass *MCLSSharedInstance;
@implementation MyClass
+ (MyClass *)sharedInstance
{
// check for existence of shared instance
// create if necessary
return MCLSSharedInstance;
}
// implementation continues
Initializing
+ (void)initialize
{
static BOOL initialized = NO;
if (!initialized) {
// Perform initialization here.
…
initialized = YES;
}
}
Class Names in Source Cod
Rectangle *anObject;
if ( [anObject isKindOfClass:[Rectangle class]] )
NSString *className;
if ( [anObject isKindOfClass:NSClassFromString(className)] )
Defining a Class
Class Interface
solution 1:
#import “ItsSuperclass.h”
@interface ClassName : ItsSuperclass
{
instance variable declarations
}
method declarations
@end
solution 2:
#import “ClassName.h”
@implementation ClassName
method definitions
@end
+ alloc{ …}
– (BOOL)isfilled{ …}
– (void)setFilled:(BOOL)flag{ …}
#import
– getGroup:group, …
{ va_list ap;
va_start(ap, group);
}
Referring to Instance Variables
– (void)setFilled:(BOOL)flag
{
filled = flag;
…
}
@interface Sibling : NSObject
{
Sibling *twin;
int gender;
struct features *appearance;
}
– makeIdenticalTwin
{
if ( !twin ) {
twin = [[Sibling alloc] init];
twin->gender = gender;
twin->appearance = appearance;
}
return twin;
}
– (BOOL)isFilled
{
return filled;
}
Referring to Other Classes
@class Rectangle, Circle;
– (void)setPrimaryColor:(NSColor *)aColor;
@interface Worker : NSObject
{
char *name;
@private
int age;
char *evaluation;
@protected
id job;
float wage;
@public
id boss;
}
– promoteTo:newPosition
{
id old = job;
job = newPosition;
return old;
}
Worker *ceo = [[Worker alloc] init];
ceo->boss = nil;
Categories and Extensions
#import “ClassName+CategoryName.h”
@implementation ClassName ( CategoryName )
// method definitions
@end
Extensions
@interface MyObject : NSObject
{ NSNumber *number;
}
– (NSNumber *)number;
@end
@interface MyObject (Setter)
– (void)setNumber:(NSNumber *)newNumber;
@end
@implementation MyObject
– (NSNumber *)number
{ return number;
}
@end
@interface MyObject : NSObject
{ NSNumber *number;
}
– (NSNumber *)number;
@end
@interface MyObject ()
– (void)setNumber:(NSNumber *)newNumber;
@end
@implementation MyObject
– (NSNumber *)number
{ return number;
}
– (void)setNumber(NSNumber *)newNumber
{ number = newNumber;
}
@end
Declared Properties
Declaring properties in a class
// MyClass.h
@interface MyClass : NSObject
{
NSString *value;
NSTextField *textField;
@private
NSDate *lastModifiedDate;
}
@property(copy, readwrite) NSString *value;
@property(retain) IBOutlet NSTextField *textField;
@end
// MyClass.m
// Class extension to declare private property
@interface MyClass ()
@property(retain) NSDate *lastModifiedDate;
@end
@implementation MyClass
@synthesize value;
@synthesize textField;
@synthesize lastModifiedDate;
// implementation continues
@end
Using Properties
– (NSString *)name;
– (void)setName:(NSString *)newName; // same as, @property NSString *name;
@interface MyClass : NSObject
{ NSString *value;
}
@property(copy, readwrite) NSString *value;
@end
@implementation MyClass
@synthesize value;
@end
@interface MyClass : NSObject
{
NSString *value;
}
@property(copy, readwrite) NSString *value;
@end
// assume using garbage collection
@implementation MyClass
@dynamic value;
– (NSString *)value {
return value;
}
– (void)setValue:(NSString *)newValue {
if (newValue != value) {
value = [newValue copy];
}
}
@end
getter=getterName, setter=setterName
getter= and setter= specify respectively the names of get and set accessors for the property. The getter must return a type matching the property’s type and take no arguments. The setter method must take a single argument of a type matching the property’s type and must return void.
readonly
Indicates that the property is read-only. The default is read/write.
readwrite
Indicates that the property should be treated as read/write. This is the default.
assign
Specifies that the setter uses simple assignment. This is the default.
retain
Specifies that retain should be invoked on the object upon assignment. (The default is assign.)
copy
Specifies that a copy of the object should be used for assignment. (The default is assign.)
nonatomic
Specifies that accessors are non-atomic. By default, accessors are atomic.( There is no keyword to denote atomic.)
Property Re-declaration
@interface MyObject : NSObject
{ NSString *language;
}
@property (readonly, copy) NSString *language;
@end
// private implementation file
@interface MyObject ()
@property (readwrite, copy) NSString *language;
@end
@implementation MyObject
@synthesize language;
@end
@property (nonatomic, copy) NSString *string;
-(void)setString:(NSString *)newString
{
if (string != newString) {
[string release];
string = [newString copy];
}
}
@interface MyClass : NSObject
{
NSMutableArray *myArray;
}
@property (nonatomic, copy) NSMutableArray *myArray;
@end
@implementation MyClass
@synthesize myArray;
– (void)setMyArray:(NSMutableArray *)newArray
{
if (myArray != newArray) {
[myArray release];
myArray = [newArray mutableCopy];
}
}
@end
Property Declaration
@interface MyClass : NSObject
{
NSString *value;
}
@property(copy, readwrite) NSString *value;
@end
// assume using garbage collection
@implementation MyClass
@dynamic value;
– (NSString *)value {
return value;
}
– (void)setValue:(NSString *)newValue {
if (newValue != value) {
value = [newValue copy];
}
}
@end
@interface MyClass : NSObject
{
NSMutableArray *myArray;
}
@property (nonatomic, copy) NSMutableArray *myArray;
@end
@implementation MyClass
@synthesize myArray;
– (void)setMyArray:(NSMutableArray *)newArray
{
if (myArray != newArray) {
[myArray release];
myArray = [newArray mutableCopy];
}
}
@end
Sample Class 1:
@interface MyClass : NSObject
{
int count;
id data;
NSString* name;
}
-(id)initWithString: (NSString*) aName;
+(MyClass*)createMyClassWithString: (NSString*)aName;
@end
@implementation MyClass
– (id)initWithString:(NSString *) aName
{
if (self = [super init]) {
count count = 0;
data = nil;
name = [aName copy];
return self;
}
}
+ (MyClass *)createMyClassWithString: (NSString *) aName
{
return [[[self alloc] initWithString:aName] autorelease];
}
@end
reference:
httpss://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/chapter_1_section_1.html
Objective begin tutor
httpss://freeborn.wordpress.com/2008/03/10/iphone-developmenet-where-to-start/
httpss://theocacao.com/document.page/510