Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3309270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:37:15+00:00 2026-05-17T21:37:15+00:00

I discovered an odd scenario that produces a compiler warning in XCode that I

  • 0

I discovered an odd scenario that produces a compiler warning in XCode that I don’t believe is a valid warning.

As an example I created two classes, ClassA & ClassB, that both have an init method called -initWithSomething:
One takes an (NSDate *) as the “something” and the other takes (NSString *)

Class A

// ClassA.h
#import <Foundation/Foundation.h>

@interface ClassA : NSObject {
}
-(id)initWithSomething:(NSDate *)something;
@end

// ClassA.m
#import "ClassA.h"

@implementation ClassA
-(id)initWithSomething:(NSDate *)something {
    if (self = [super init]) {
    }
    return self;
}
@end

Class B

// ClassB.h
#import <Foundation/Foundation.h>

@interface ClassB : NSObject {
}
-(id)initWithSomething:(NSString *)something;
@end

// ClassB.m
#import "ClassB.h"

@implementation ClassB
-(id)initWithSomething:(NSString *)something {
    if (self = [super init]) {
    }
    return self;
}
@end

Implementation of another class that uses both ClassA & ClassB

#import "ExampleClass.h"
#import "ClassA.h"
#import "ClassB.h"

@implementation ExampleClass

-(void)doSomething {
    NSDate *date = [NSDate date];
    NSString *string = [NSString stringWithFormat:@"Test"];

    ClassA *classA = [[ClassA alloc] initWithSomething:date];
    ClassB *classB = [[ClassB alloc] initWithSomething:string]; // Produces "Incompatible pointer types sending 'NSString *' to parameter of type 'NSDate *'
    ClassB *classB2 = [[ClassB alloc] initWithSomething:[NSString stringWithFormat:@"Test"]]; // Does NOT produce a warning
    ClassB *classB3 = [[ClassB alloc] initWithSomething:@"Test"]; // Produces the same warning as above.

    [classA release];
    [classB release];
    [classB2 release];
    [classB3 release];
}

Is this a compiler bug? Doesn’t seem like either of those lines should produce a warning, especially since the line where “classB2” is initted produces no warnings.
This code actually works fine, the correct class’ -initWithSomething: is called and passed the appropriate type of argument.

Clearly, more explicit method names will avoid the issue but I’d like to know why the compiler isn’t able to handle this.

Note:
I should add that this only seems to occur with -init methods, any other instance or class functions do not seem to produce the warning.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:37:15+00:00Added an answer on May 17, 2026 at 9:37 pm

    I think that the problem is that +alloc returns a generic id.

    This means that any method can be called on it, and the compiler will see that the first method imported that has the signature -initWithSomething is for class A, which expects an object of type NSDate *.

    Also, I do believe that the method +stringWithFormat returns an id which can be compatible with NSDate.

    EDIT:

    A simple solution to this problem:

    @interface ClassA
    
    +(ClassA *) typeSafeAlloc;
    
    // ...
    @end
    
    @implementation ClassA
    
    +(ClassA *) typeSafeAlloc
    {
        // self is the class variable,  which is the same as:
        // return [ClassA alloc];
        return [self alloc];
    }
    @end
    

    And repeat the process with ClassB (with typeSafeAlloc returning a ClassB object)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I discovered that one of the tables of a legacy db I'm working on
We recently discovered that the Google Maps API does not play nicely with SSL.
Today I discovered that my fresh installation of Apache HTTP Server is able to
I've discovered that any time I do the following: echo '<a href=http:// title=bla>huzzah</a>'; I
I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as
I've discovered that cElementTree is about 30 times faster than xml.dom.minidom and I'm rewriting
I discovered template metaprogramming more than 5 years ago and got a huge kick
I discovered to have some problem to fully understand callbacks scoping when trying to
I recently discovered the genshi.builder module. It reminds me of Divmod Nevow's Stan module.
Having recently discovered this method of development, I'm finding it a rather nice methodology.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.