Martin R がコメントで述べたように、現時点で最良の選択肢は、クラスにfirstBObject
プロパティを作成することです。AObject
AObject.h/m
@class BObject;
@interface AObject : NSObject
+ (AObject*)aObjectWithBObjects:(NSArray*)bObjects;
@property NSArray *bObjects;
@property (nonatomic, readonly) BObject *firstBObject;
@end
@implementation AObject
+ (AObject*)aObjectWithBObjects:(NSArray*)bObjects
{
AObject *ao = [[self alloc] init];
ao.bObjects = bObjects;
return ao;
}
- (BObject*)firstBObject
{
return [self.bObjects count] > 0 ? [self.bObjects objectAtIndex:0] : nil;
}
@end
BObject.h/m
@interface BObject : NSObject
+ (BObject*)bObjectWithName:(NSString*)name;
@property NSString *name;
@end
@implementation BObject
+ (BObject*)bObjectWithName:(NSString *)name
{
BObject *bo = [[self alloc] init];
bo.name = name;
return bo;
}
@end
使用法:
NSArray *aobjects = @[
[AObject aObjectWithBObjects:@[
[BObject bObjectWithName:@"A1B1"],
[BObject bObjectWithName:@"A1B2"],
[BObject bObjectWithName:@"A1B3"],
[BObject bObjectWithName:@"A1B4"]
]],
[AObject aObjectWithBObjects:@[
[BObject bObjectWithName:@"A2B1"],
[BObject bObjectWithName:@"A2B2"],
[BObject bObjectWithName:@"A2B3"],
[BObject bObjectWithName:@"A2B4"]
]],
[AObject aObjectWithBObjects:@[
[BObject bObjectWithName:@"A3B1"],
[BObject bObjectWithName:@"A3B2"],
[BObject bObjectWithName:@"A3B3"],
[BObject bObjectWithName:@"A3B4"]
]]
];
NSLog(@"%@", [aobjects valueForKeyPath:@"firstBObject.name"]);
結果
(
A1B1、
A2B1、
A3B1
)