Saturday, January 07, 2012

iOS basics

- Variables: int, float, double
- Operators: +, -, *, /, %
- Conditional statements: if-else, switch-case
- Loops: for, while, do-while
- printf() function
- viewDidLoad event


Example:
int i = 1;
float f = 2.3;
printf("This is a string with integer: %d and a floating point number: %f", i, f);
NSLog(@"This is a string with integer: %d and a floating point number: %f", i, f);



Constants:
.h
extern const int MY_NUMBER;
.m
const int MY_NUMBER = 123;



Outlets
in .h
UITextField *tf;
@property (nonatomic, retain) IBOutlet UITextField *tf;

in .m
@synthesize tf;
tf.text = @"Hello textfield";

in .xib
Reload class files, if necessary
File's Owner > Ctrl+Click and drag to textfield in "View" > New reference (outlet)

Actions
in .h
-(IBAction) whatToDoWhenButtonClicked:(id)sender;

in .m
-(IBAction) whatToDoWhenButtonClicked:(id)sender {
// do what ever
tf.text = @"button clicked";
}

in .xib
Reload class files, if necessary
Ctrl+click button in "View" and drag to "File's Owner" > "whatToDoWhenButtonClicked" (Action) TouchUpInside

NSString
NSString *str = [NSString stringWithFormat: @"string %@ with %d and %f", someWords, i, f]
float f = [str floatValue]
int i = [str intValue]



Object-Oriented Programming (OOP)
@interface
@property
@implementation
@synchronise
@end
methods








Creating a custom class, MyClass

  1. MyClass.h
    @interface MyClass : NSObject {
       float myfloat;
       int myint;
    }
    @property float myfloat;
    @property int myint;
    -(void) myMethodWithParameter:(float) param1
    @end
  2. MyClass.m
    #import "MyClass.m"
    @implementation MyClass
    @synthesize myfloat;
    @synthesize myint;
    -(void) myMethodWithParameter:(float) param1
    {
       NSLog(@"this is my method with %f", param1);
    }
    @end

Instantiation of an object and using the object

MyClass *myObject = [MyClass alloc];
[myObject init]; // or [[MyClass alloc] init];
[myObject myMethodWithParameter:4.5];
[myObject release];






NSNumber
NSNumber *fNum = [NSNumber numberWithFloat:12.3];
float f = [fNum floatValue];
NSString *str = [fNum stringValue];
NSNumber reference

NSArray & NSMutableArray
NSArray *names01 = [NSArray arrayWithObjects: @"Beng", @"Siti", @"Dinesh"];
NSMutableArray *names02 = [NSMutableArray arrayWithObjects: @"Beng", @"Siti", @"Dinesh"];
int number = [names01 count];
NSString *firstName = [names01 objectAtIndex:0];
OR for fast enumeration
for(NSString *n in names01)
{
NSLog(@"The name is %@", n);
}

No comments: