Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Can I use a class convenience method in an assembly initializer? #312

Closed
iosdev-republicofapps opened this issue Jan 24, 2015 · 1 comment

Comments

@iosdev-republicofapps
Copy link

Suppose I have my Knight class as follows:

@class Quest;

@interface Knight : NSObject
- (id)initWithQuest:(Quest *)quest;
- (void)importantPostInitializationSetup;
@end

Let's say that importantPostInitializationSetup can't be done inside the initializer for some reason. Now, I know I could code my assembly like the following:

- (Knight *)basicKnight
{
    return [TyphoonDefinition withClass:[Knight class] 
        configuration:^(TyphoonDefinition* definition) {

        [definition useInitializer:@selector(initWithQuest:) 
            parameters:^(TyphoonMethod *initializer) {

            [initializer injectParameterWith:[self defaultQuest]];

        }];

        [definition injectMethod:@selector(importantPostInitializationSetup) 
            parameters:^(TyphoonMethod *method) {
        }];
    }];
}

But this exposes the importantPostInitializationSetup method. I'd rather use a class convenience method like the following:

@class Quest;

@interface Knight : NSObject
+ (instancetype)knightWithQuest:(Quest *)quest;
@end

@implementation Knight
- (instancetype)initWithQuest:(Quest *)quest { // as before }
- (void)importantPostInitializationSetup { // as before }

+ (instancetype)knightWithQuest:(Quest *)quest
{
    Knight *knight = [[Knight alloc] initWithQuest:quest];
    [knight importantPostInitializationSetup];
    return knight;
}
@end

Is it possible to use + knightWithQuest inside a Typhoon definition?

Thanks!

@jasperblues
Copy link
Member

Yes! No problem at all. All of the Objective-C idioms work. An initializer can be a class/factory method (also works with class clusters) or a regular initializer.

Swift Gotcha! In Swift when you create a factory method that points to an init, sometimes Swift will just directly map to the init, so the extra code in the factory/class initializer won't run!

Please in future post general questions to StackOverflow as it helps other users, and shares the support load, so that we can keep improving Typhoon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants