开发者

xcode 4.1 yellow triangles?

开发者 https://www.devze.com 2023-04-12 11:47 出处:网络
When I compile my project the x-code environment tells me that there are multiple methods named myInit:: found, but there aren\'t.

When I compile my project the x-code environment tells me that there are multiple methods named myInit:: found, but there aren't.

I've created a sub-class of UIControl, and wanted a custom initialiser. I called it myInit, defined like so:

- (id)myInit:(float)Left:(ExecData *)Exec
{
   self = [super initWithFrame:CGRectMake(Left, 0.0, 120.0, 200.0)];

    if (self) 
    {        
       开发者_如何学JAVA exec = Exec;
        [self initialise]; // another method
    }

    return self;
}

My class is called ExecSummaryControl. So, to instantiate an object based on this class, I type:

ExecSummaryControl *control = [[ExecSummaryControl alloc]myInit:20.0:myExec];

where myExec is an object of type ExecData.

It all works just as I want, but the x-code development environment puts a yellow triangle against the instantiation line, reporting multiple instances of myInit found. There are no multiple instances, and the code runs fine. Is it perhaps that x-code has compiled old code somewhere that's cached? I have attempted to Project -> Clean, but no joy.

Anyone any ideas how I can clean out this erroneous warning?


You should really look over Learning Objective-C: A Primer: Methods and Messaging as well as The Objective-C Programming Language: Object Messaging.

Your custom init method as shown has unnamed parameters, which is, well, bizarre to say the least.

In other words, if we put some spacing in your method signature, it is:

- (id)myInit:(float)Left :(ExecData *)Exec;

The second parameter does not have a label or name before the colon of :(ExecData *). I would consider renaming the method to something like this:

- (id)initWithLeftEdgeAtX:(CGFloat)left execData:(ExecData *)anExecData
{
   self = [super initWithFrame:CGRectMake(left, 0.0, 120.0, 200.0)];

    if (self) 
    {        
        exec = anExecData;
        [self initialise]; // another method
    }

    return self;
}

The reason you're getting that warning from the compiler is because you likely have other classes in your project that also have a -myInit:: method that also has unnamed parameters, such as the following:

- (id)myInit:(NSString *)someString :(SomeClass *)anUnnamedParameter;

The compiler is trying to do some sanity checks for you by seeing if you are calling a method on an instance of an object that actually implements the method (aka "responds to selector"). For example, you wouldn't want to call NSArray's -objectAtIndex: method on an instance of NSString. Because you're using unnamed parameters, the method signature is -myInit:: in more than one class, so the compiler is saying it's not sure what one you mean. That's even more reason why you should make sure you used named parameters like how I suggested.

Then the initialization would look like

ExecSummaryControl *control= [[ExecSummaryControl alloc] initWithLeftEdgeAtX:20.0
                                                                execData:myExec];

The method signature would then be -initWithLeftEdgeAtX:execData:, which makes it easier for the compiler to help check upfront whether you appear to be calling the methods properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号