ObjC type error

Status
Not open for further replies.
In the same file:

Code:
-(CGFloat) lowerBound:(UIView*) v
{
	return v.frame.size.height + v.frame.origin.y;
}

Code:
details.frame = CGRectMake(0, [self lowerBound:name], 0, 0);

Where name and details are both UILabel.

The program fails at compiling with the error:
@"Incompatible type for argument 2 of CGRectMake"

I've been trying this, that and the other. What's going on? All arguments of CGRectMake supposedly take CGFloat.
 

Jonathon

Ars Legatus Legionis
17,645
Subscriptor
topham":1m3jebnw said:
Doesn't matter if they are in the same file. It does matter which comes first, or (alternatively) if you've specified the method in your header file.

Put

-(CGFloat) lowerBound:(UIView*)v;

in your header file.
To expand on this: Objective-C (for Objective-C messages; pure C works differently) assumes a return type of (id) for selectors which weren't previously defined (either in a header file or above the current location in the source file). Since CGFloat is not an Objective-C object (it's just a plain C struct), id is not a compatible type.

By default, GCC (and Clang, I think) will warn about this particular scenario. You're probably seeing it now (although it may be suppressed by the error on the same line). It's generally wise to pay attention to and correct compiler warnings: they'll usually indicate a latent (sometimes subtle) bug in your program that could come back to bite you later.
 

Ember

Ars Centurion
346
Subscriptor++
A few nitpickings.
Code:
return v.frame.size.height + v.frame.origin.y;
The above is bad, it makes call to [v frame] twice. You'd better change as below.
Code:
    CGRect frame_rect = v.frame;
    return frame_rect.size.height + frame_rect.origin.y;
Or better,
Code:
    return CGRectGetMaxY(v.frame);

It works on any UIView objects in general, why make it an instance method of (probably) unrelated class?
Just make it a C functions like below.
Code:
CGFloat lowerBoundOfView(UIView *v)
{
    return CGRectGetMaxY(v.frame);
}

Or make your own category.
Code:
@interface UIView (MyExtenstions)
- (CGFloat)lowerBound;
@end

@implementation UIView (MyExtensions)
- (CGFloat)lowerBound
{
    return CGRectGetMaxY(self.frame);
}
@end

// And use as below.
details.frame = CGRectMake(0, [name lowerBound], 0, 0);
 
Status
Not open for further replies.