Thursday, January 16, 2014

Creating Classes and Objects


Web hosting
Based on my understanding, this is how I create a class and function in objective c. See the code below.

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, strong)NSString *fname;
@property (nonatomic, getter = getBal, setter = setBal:)double balance;

- (void)setName:(NSString *)name;
- (NSString *)functionName:(NSString *)parameter1 :(NSString *)parameter2;
@end

Person. m

#import "Person.h"

@implementation Person
@synthesize fname, balance;

- (NSString *)setName:(NSString *)name{
  fname = name;
}

- (NSString *)functionName:(NSString *)parameter1 :(double)parameter2{
fname = parameter1;
balance = parameter2;
}

@end


As you see that in Person.h file we are just declaring the variable and functions but in Person.m file we implement thus variable and functions.

So in you other class, we just name Other.m file. You can call the function through this code below.


#import "Other"
#import "Person.h"

//@interface SampleViewController ()
//
//@end

@implementation SampleViewController
Person *getInfo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    getInfo = [[Person alloc]init];
    
    [getInfo setName:@"Gaudicos, Alberto Jr."];
    [getInfo functionName:@"Parameter1" :@"Parameter2"];

    
}


@end

Web hosting
That's it, how I understand basic in creating class and functions in oboective c programming.


No comments:

Post a Comment