Question

I'm attempting to use resolveClassMethod: and object_addMethod to add a new method. Things seem to be working fine in general, but the parameter doesn't seem to be coming through.

When I pass in a parameter, such as:

UIFont *font1 = [UIFont academyEngravedLetPlainOfSize:12.0f];

I get 0.0 instead of 12.0 for the font size in my dynamic method. Here's the output from the sample below:

createNamedFontIMP(UIFont, academyEngravedLetPlainOfSize:, 0.000000)

What am I doing wrong here?

@interface UIFont (NTNamedFonts)

+(UIFont *)academyEngravedLetPlainOfSize:(int)size NS_AVAILABLE_IOS(6_0);

@end

@implementation UIFont (NTNamedFonts)

NSString *targetSelectorName = @"academyEngravedLetPlainOfSize:";
NSString *targetFontName = @"AcademyEngravedLetPlain";

+(BOOL)resolveClassMethod:(SEL)sel
{
    NSString *selectorName = NSStringFromSelector(sel);

    if ( [selectorName isEqualToString:targetSelectorName] )
    {
        Class metaClass = object_getClass(self);
        class_addMethod(metaClass, sel, (IMP)createNamedFontIMP, "@#:f");
        return YES;
    }
    else
        return [super resolveClassMethod:sel];
}

static id createNamedFontIMP(Class class, SEL _cmd, CGFloat size)
{
    NSLog(@"createNamedFontIMP(%@, %@, %f)", NSStringFromClass(class), NSStringFromSelector(_cmd), size);

    UIFont *font = [class fontWithName:targetFontName size:size];

    return font;
}

@end
Was it helpful?

Solution

You pass an integer to a float. Please show the method declaration.

BTW: What's the reason for your code? A category seems to be the better approach.

BTW 2: If I remember correctly, you have to use double.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top