Reputation: 19
I'm using the latest version of xcode 5.
I get this error twice in the following code:
+ (SemiSecretFont *)fontWithName:(NSString *)name
size:(CGFloat) size;
{
//dynamically search for a class with this name
Class klass = NSClassFromString([NSString stringWithFormat:@"%@Font", name]);
//NSLog(@"looking for font: %@", name);
// NSLog(@"klass: %@", klass);
SemiSecretFont * font = nil;
if (klass)
font = [[[klass alloc] initWithSize:size] autorelease]; //error occurs here
return font;
}
- (id) fontWithSize:(CGFloat)s
{
Class klass = [self class];
SemiSecretFont * f = nil;
f = [[[klass alloc] initWithSize:s] autorelease]; //error occurs here again
return f;
}
The error: Sending 'CGFloat' (aka 'float') to parameter incompatible type 'CGSize' (aka 'struct CGSize')
I also get a warning: Multiple methods named 'initWithSize:' found
Update:
Here's my initWithSize declaration code...
//this is not meant to be instantiated directly!
- (id) initWithSize:(CGFloat)fontsize
{
if ((self = [super init])) {
size = fontsize;
font = nil;
}
return self;
}
Upvotes: 1
Views: 7628
Reputation: 539805
This seems to be from https://github.com/ericjohnson/canabalt-ios, and
+ (SemiSecretFont *)fontWithName:(NSString *)name size:(CGFloat) size;
seems to be a factory method, that returns an instance of SemiSecretFont
or some
subclass. But when compiling
font = [[[klass alloc] initWithSize:size] autorelease]; //error occurs here
the compiler does not know that kclass
is the SemiSecretFont
class (or a subclass) which has
a method
- (id) initWithSize:(CGFloat)fontsize;
Theoretically, it could be an instance of NSTextContainer
, which has the method
- (id)initWithSize:(CGSize)size; // designated initialiser
To resolve that ambiguity, you can add an explicit cast:
font = [[(SemiSecretFont *)[klass alloc] initWithSize:size] autorelease];
Upvotes: 3