I have a class “ABC” and its method which returns non autoreleases object of that class.
@interface ABC:NSObject
+(ABC *)aClassMethodReturnsObjectWhichNotAutoreleased;
@end
@implementation ABC
+(ABC *)aClassMethodReturnsObjectWhichNotAutoreleased{
ABC *a = [[ABC alloc]init];
return a;
}
@end
If I have a protocol Foo.
@Protocol Foo
@required
-(void)abc;
@end
My ABC class is “not” confirming Foo protocols.
1st call
id<Foo> obj = [ABC aClassMethodReturnsObjectWhichNotAutoreleased]; //show warning
It shows warning “Non Compatible pointers..” thats good.Abc did not confirm protocol Foo
BUT
2nd call
id<Foo> obj = [NSArray arrayWithObjects:@"abc",@"def",nil]; // It will "not" show warning as it will return autorelease object.NSArray don't confirm protocol Foo
In first call compiler gives warning and in second call compiler is not giving any warning.I think that is because i am not returning autorelease object.
Why is compiler not giving warning in 2nd call as NSArray is also not confirming FOO
Thanks in advance
In your first example, the return value is a specific type so the compiler can verify the assignment.
In the second example, the
NSArray arrayWithObjects:method has a return type ofid. You can assign an object of typeidto a variable of any type. The compiler has no way to verify that what you are doing is truly correct or not.This issue has nothing to do with autoreleased objects. It’s all about the data types.
idis a kind of catch-all type that can be anything.