Objective C は初めてで、複素数を定義するクラスを作成しようとしています。コードは問題ないように見えますが、コンソールに出力すると、インスタンス変数の値が 0 になります。
コードは次のとおりです。
//
// 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
デバッグ目的で App Delegate に以下を追加しました。
- (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;
}
しかし、コンソールは毎回 0 を返します。ヘルプ?