LoginSignup
7
7

More than 5 years have passed since last update.

クラスメソッド内でselfを使う

Last updated at Posted at 2012-08-05

親クラスに書いておくだけで
[HogeHoge hogeWithDictionary];
[Poi hogeWithDictionary];
によって子のオブジェクトが作れます。

hoge.m
@interface Hoge : NSObject
+ (id)hogeWithDictionary:(NSDictionary *)dic;
@end

@interface HogeHoge : Hoge
@end

@interface Poi : Hoge
@end

@implementation Hoge
+ (id)hogeWithDictionary:(NSDictionary *)dic
{
    Hoge  *hoge = [[[[self class] alloc] init] autorelease];
    [hoge setUp:dic];

    return hoge;
}

- (void)setUp:(NSDictionary *)dic
{
    //処理
    NSLog(@"Hoge処理");
}
@end

@implementation HogeHoge
- (void)setUp:(NSDictionary *)dic
{
    [super setUp:dic];
    //処理
    NSLog(@"HogeHoge処理");
}
@end


@implementation Poi
- (void)setUp:(NSDictionary *)dic
{
    //処理
    NSLog(@"Poi処理");
}
@end

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7