私は現在、クラス 'Array' とクラス 'PPCalcVals' の 2 つのクラスに分割されたプロジェクトに取り組んでいます。追加される他のクラスも配列にアクセスする必要があるため、NSMutableArray とサブクラス (PPCalcVals クラスから始まる) を含む配列クラスを作成するのが最善であると考えました。 ' class needs to access the elements of the array in the superclass 'Array'. (これが間違ったアプローチである場合は修正してください。) 前述のように、プログラム全体は C で記述され、適切に機能しますが、GUI を作成し、最終的には OSX を作成します。または IOS アプリケーション Objecitve C を使用した OOProgramming の学習を開始しました。とにかく、スーパークラスの配列内のオブジェクトを参照すると、出力される値は「null」だけです。これは私が本当に欲しいものではありません。コードは次のとおりです。
メインルーチン:
#import <Foundation/Foundation.h>
#import "Array.h"
#import "PPCalcVals.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Array *prices = [[Array alloc]initWithName:@0];
PPCalcVals *myVals = [[PPCalcVals alloc]init];
[prices addValue:@12];
[prices addValue:@13];
[prices addValue:@14];
[prices addValue:@15];
[prices addValue:@15];
[prices print];
[myVals print];
}
return 0;
}
array.h ファイル:
#import <Foundation/Foundation.h>
@interface Array : NSObject
{
NSMutableArray *prices;
}
-(id) initWithName: (NSNumber *) values;
-(void) addValue: (NSNumber *) value;
-(void) print;
-(NSMutableArray *) prices;
@end
配列.m
#import "Array.h"
@implementation Array
-(id) initWithName:(NSNumber *)values
{
self = [super init];
if(self)
{
prices = [NSMutableArray array];
}
return self;
}
-(void) addValue: (NSNumber *) value
{
[prices addObject:value];
}
-(void) print
{
NSLog(@"%@",prices);
}
-(NSMutableArray *)prices
{
return prices;
}
@end
PPCalcVals.h:
#import "Array.h"
@interface PPCalcVals : Array
@property id high,low,open,close;
-(void) setHigh:(NSMutableArray *)h setLow:(NSMutableArray *)l; //set high and low
-(void) setOpen:(NSMutableArray *)o setClose:(NSMutableArray *)c; //set open and close
-(void) sort; //sort array
-(void) print; //debugging tool
@終わり
PPCalcVals.m:
#import "PPCalcVals.h"
@implementation PPCalcVals
@synthesize high,low,open,close;
-(void) setOpen:(NSMutableArray *)o setClose:(NSMutableArray *)c
{
o = prices[0];
c = prices[2];
open = o;
close = c;
}
-(void) sort;
{
[prices sortedArrayUsingComparator:^(NSString *str1, NSString *str2) {
return [str1 compare:str2 options:NSNumericSearch];
}];
}
-(void) setHigh:(NSMutableArray *)h setLow:(NSMutableArray *)
{
h = prices[0];
l = prices[2];
high = h;
low = l;
}
-(void) print
{
NSLog(@"open: %@",open);
NSLog(@"close: %@",close);
NSLog(@"high: %@",high);
NSLog(@"low: %@",low);
}
@end
実行中のプログラムは以下のみを出力します。
2013-08-05 10:21:08.546 prog1[1314:303] (
12,
13,
14,
15,
15
)
open: (null)
close: (null)
high: (null)
low: (null)
これはおそらく本当に基本的な質問だと思いますが、あなたの助けに感謝します.