Defining a Class in Objective C, XCode
        Posted  
        
            by Brett
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Brett
        
        
        
        Published on 2010-06-03T14:58:29Z
        Indexed on 
            2010/06/03
            15:04 UTC
        
        
        Read the original article
        Hit count: 514
        
Hello;
I am new to Objective C, and am trying to write a class that defines a complex number. The code seems fine but when I print to the console, my values for instance variables are 0.
Here is the code:
//
//  ComplexNumber.h
//  Mandelbrot Set
//
//  Created by Brett on 10-06-02.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface ComplexNumber : NSObject {
 double real;
 double imaginary;
}
// Getters
-(double) real;
-(double) imaginary;
// Setters
-(void)setReal: (double) a andImaginary: (double) b;
//Function
-(ComplexNumber *)squared;
@end
//
//  ComplexNumber.m
//  Mandelbrot Set
//
//  Created by Brett on 10-06-02.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ComplexNumber.h"
#import <math.h>
#import <stdio.h>
@implementation ComplexNumber
-(double)real{
 return self->real;
}
-(double)imaginary{
 return self->imaginary;
}
-(void)setReal: (double) a andImaginary: (double) b{
 self->real=a;
 self->imaginary=b;
}
-(ComplexNumber *)squared{
 double a = pow(real,2);
 double b = pow(imaginary, 2);
 double c = 2*real*imaginary;
 ComplexNumber *d;
 [d setReal:(a-b) andImaginary: c];
 return d;
}
@end
In the App Delegate for debugging purposes I added:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
 ComplexNumber *testNumber = [[ComplexNumber alloc] init];
 [testNumber setReal:55.0 andImaginary:30.0];
 NSLog(@"%d", testNumber.real);
    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
 return YES;
}
But the console returns 0 everytime. Help?
© Stack Overflow or respective owner