Re: How to avoid warning?

2013-01-23 Thread Uli Kusterer
On Jan 22, 2013, at 8:12 PM, Jens Alfke j...@mooseyard.com wrote:
 @interface SomeFictionalClassName
 - (id) initWithManager: (Foo*)manager;
 @end

That feels a bit dirty to me. I'd recommend using a protocol instead. As long 
as the compiler has seen the method *somewhere*, it will consider it having 
been declared. So

@protocol AvoidCompilerWarning
- (id) initWithManager: (Foo*)manager;
@end

is, for the purposes of this particular discussion, equivalent to Jens's 
fictional class interface, but doesn't imply that such a class might exist to a 
(human) reader.

Cheers,
-- Uli Kusterer
The Witnesses of TeachText are everywhere...
http://www.zathras.de


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-23 Thread Uli Kusterer

On Jan 22, 2013, at 8:28 PM, Andy Lee ag...@mac.com wrote:
 To be extra fail-safe, you might want to perform a cast to be sure the right 
 initWithManager: gets called:
 
 if ([myClass conformsToProtocol:@protocol(MyProtocol)])
   myObj = [(id MyProtocol)[myClass alloc] initWithManager:self];
 else
   myObj = [[myClass alloc] init];


 This only works for classes that actually declare themselves to be in 
conformance with this protocol, though. Even if a class implements all the 
methods in a protocol, conformsToProtocol: will return NO if it doesn't have 
the protocol name on the @interface (or on the @implementation line).

IMO the nicest way to do this (if you can't make all your classes conform to 
the protocol by declaring them as @interface Foo : NSObject 
UKCanInitWithManagerProtocol, which would definitely be the cleanest 
approach), would be:

@protocol UKCanInitWithManagerProtocol
-(id)   initWithManager: (Foo*)inManager;
@end

...

if( [myClass respondsToSelector: @selector(initWithManager:)] )
myObj = [(idUKCanInitWithManagerProtocol)[myClass alloc] 
initWithManager: self];
else
myObj = [[myClass alloc] init];

Though I would recommend choosing a different name. E.g. -initWithBarManager: 
if the manager class is called UKBarManager. Manager alone is too generic a 
word, and someone might have declared it in ObjC++ and taking a C++ object and 
then you're screwed.

Cheers,
-- Uli Kusterer
The Witnesses of TeachText are everywhere...
http://www.zathras.de


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-23 Thread Andy Lee
On Jan 23, 2013, at 8:22 AM, Uli Kusterer witness.of.teacht...@gmx.net wrote:
 @protocol UKCanInitWithManagerProtocol
 -(id) initWithManager: (Foo*)inManager;
 @end
 
 ...
 
 if( [myClass respondsToSelector: @selector(initWithManager:)] )
   myObj = [(idUKCanInitWithManagerProtocol)[myClass alloc] 
 initWithManager: self];
 else
   myObj = [[myClass alloc] init];

Well, the extra-super-defensive approach would be to not only check 
respondsToSelector:, but to check that the method signature of the object's 
implementation matches what's declared in the protocol. :)

But yeah, you can't rely on conformsToProtocol: if you don't have the luxury of 
making the relevant classes conform to the protocol.

--Andy

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Dave

That's wont help because myClass is a variable created from a String.

as in:

-void methodXXX:(NSString*) theClassName

myClass = NSClassFromString(theClassName);
if (myClass == nil)
return;

myObj = [[myClass alloc] initWithManager:self]];
}


To whoever said write better code that has types defined, this code  
is fine. You can't know the Class here which is why I'm checking to  
see if the Class supports the selector first. I could use  
performSelector but not sure how that would work with the alloc method:


myObj = [[myClass alloc] performSelector(@selector 
(initWithManager:) withObject:self];


Would this work?

Cheers
Dave


On 21 Jan 2013, at 18:42, Quincey Morris wrote:


On Jan 21, 2013, at 10:14 , Dave d...@looktowindward.com wrote:


myObj = [[myClass alloc] initWithManager:sel]];


I get a warning on the initWithManager: statement (Obviously), how  
to avoid the warning or otherwise fix it?


You need to #import a header file with an @interface declaration  
for the 'initWithManager:' method.


The rule is that when the compiler sees a message send to a  
receiver of type 'id' (which is the return type of 'alloc'), it  
needs to have seen *some* declaration of that method. If it has  
seen more than one declaration, BTW, the declarations must all be  
compatible in terms of parameter and return types.




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Dave


On 21 Jan 2013, at 18:44, Steve Sisak wrote:


At 6:14 PM + 1/21/13, Dave wrote:
if (class_RespondsToSelector(myClass,@selector(initWithManager:)  
== NO)

myObj = [[myClass alloc] init];
else
myObj = [[myClass alloc] initWithManager:sel]];

I get a warning on the initWithManager: statement (Obviously), how  
to avoid the warning or otherwise fix it?


You could try declaring initWithManager: in a category on the class  
visible only to your implementation code. (i.e. at the top of  
your .m file)


The class name is passed in as a string and the class is formed from  
that, so I can't pre-declare it. Please see my other reply.


Cheers
Dave


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Dave


On 21 Jan 2013, at 22:12, Jens Alfke wrote:



On Jan 21, 2013, at 10:14 AM, Dave d...@looktowindward.com wrote:

if (class_RespondsToSelector(myClass,@selector(initWithManager:)  
== NO)


Off-topic: instead of using the Obj-C runtime’s C API, you can  
express this as
	if ([myClass instancesRespondToSelector: @selector 
(initWithManager:)] == NO)


—Jens


Ok, cool, thanks for that, I still have the same problem though when  
I call the initWithManager.


Cheers
Dave

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread Jean Suisse

 myObj = [[myClass alloc] performSelector(@selector(initWithManager:) 
 withObject:self];
 
 Would this work?
 


You could do :

id myObj =[myClass alloc];
myObj = [myObj performSelector(@selector(initWithManager:) withObject:myObj];
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Jean Suisse

On 22 janv. 2013, at 18:34, Jean Suisse jean.li...@gmail.com wrote:

 
 myObj = [[myClass alloc] performSelector(@selector(initWithManager:) 
 withObject:self];
 
 Would this work?
 
 
 
 You could do :
 
 id myObj =[myClass alloc];
 myObj = [myObj performSelector(@selector(initWithManager:) 
 withObject:myObj];


[Sorry, I did hit send by mistake]

This (above) implies that the manager is self. Not that myObj is used as self 
when calling init.

Or you could declare a protocol myProtocol that defines initWithManager: and 
have the following declaration for myObj:
id myProtocol myObj = …
This would avoid the use of @selector.
You could also test if myObj responds to the selector selector just after 
calling alloc.

Jean

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread Keary Suska

On Jan 22, 2013, at 10:23 AM, Dave wrote:

 
 On 21 Jan 2013, at 18:44, Steve Sisak wrote:
 
 At 6:14 PM + 1/21/13, Dave wrote:
 if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)
 myObj = [[myClass alloc] init];
 else
 myObj = [[myClass alloc] initWithManager:sel]];
 
 I get a warning on the initWithManager: statement (Obviously), how to avoid 
 the warning or otherwise fix it?
 
 You could try declaring initWithManager: in a category on the class visible 
 only to your implementation code. (i.e. at the top of your .m file)
 
 The class name is passed in as a string and the class is formed from that, so 
 I can't pre-declare it. Please see my other reply.


If in fact myClass may be any number of classes that may implement certain 
methods, the right way to do this is using a protocol. Protocols are designed 
to fit this purpose. I don't recall, however, the exact method to hint to the 
compiler enough to avoid the warning. Importing the protocol header may be 
sufficient.

HTH,

Keary Suska
Esoteritech, Inc.
Demystifying technology for your home or business


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Dave


On 22 Jan 2013, at 17:34, Jean Suisse wrote:



myObj = [[myClass alloc] performSelector(@selector 
(initWithManager:) withObject:self];


Would this work?




You could do :

id myObj =[myClass alloc];
myObj = [myObj performSelector(@selector(initWithManager:)  
withObject:myObj];


Thanks for this, I was wondering if that would work too. I want to  
pass self (the instance of the current class) to initWithManager  
though. So can I just use:


myObj = [myObj performSelector(@selector(initWithManager:)  
withObject:self];


Thanks
Dave


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Dave


On 22 Jan 2013, at 18:07, Keary Suska wrote:



On Jan 22, 2013, at 10:23 AM, Dave wrote:



On 21 Jan 2013, at 18:44, Steve Sisak wrote:


At 6:14 PM + 1/21/13, Dave wrote:
if (class_RespondsToSelector(myClass,@selector(initWithManager:)  
== NO)

myObj = [[myClass alloc] init];
else
myObj = [[myClass alloc] initWithManager:sel]];

I get a warning on the initWithManager: statement (Obviously),  
how to avoid the warning or otherwise fix it?


You could try declaring initWithManager: in a category on the  
class visible only to your implementation code. (i.e. at the top  
of your .m file)


The class name is passed in as a string and the class is formed  
from that, so I can't pre-declare it. Please see my other reply.



If in fact myClass may be any number of classes that may implement  
certain methods, the right way to do this is using a protocol.  
Protocols are designed to fit this purpose. I don't recall,  
however, the exact method to hint to the compiler enough to avoid  
the warning. Importing the protocol header may be sufficient.


This has to work with classes that exist already as well as classes  
that don't. If initWithManager is defined in the class in question  
knows what it is being called like this, if not then it defaults to  
the regular NSObject init.


For instance it could be NSString or NSArray that is the class, in  
which case it won't have initWithManager so it calls init instead.


All the Best
Dave


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Jens Alfke

On Jan 22, 2013, at 9:24 AM, Dave d...@looktowindward.com wrote:

 Ok, cool, thanks for that, I still have the same problem though when I call 
 the initWithManager.

Others answered this already. To recap: The compiler needs to see a declaration 
of an -initWithManager: method before it parses this line. So you have to 
either #import the header that declares that method, or if there’s no such 
header, put a category interface at the top of this .m file that defines the 
method.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread Dave


On 22 Jan 2013, at 18:26, Jens Alfke wrote:



On Jan 22, 2013, at 9:24 AM, Dave d...@looktowindward.com wrote:

Ok, cool, thanks for that, I still have the same problem though  
when I call the initWithManager.


Others answered this already. To recap: The compiler needs to see a  
declaration of an -initWithManager: method before it parses this  
line. So you have to either #import the header that declares that  
method, or if there’s no such header, put a category interface at  
the top of this .m file that defines the method.


That won't work either, you have to use performSelector:withObject:  
which seems to do the trick nicely!


Cheers
Dave


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread John McCall
On Jan 22, 2013, at 9:23 AM, Dave d...@looktowindward.com wrote:
 That's wont help because myClass is a variable created from a String.

But it will help, because Quincey's suggestion is not contingent on the type of 
the object that you send the message to.  The compiler is merely asking that 
you have declared a method with that selector *somewhere* in this translation 
unit so that it knows (or at least has a really good guess) about the formal 
signature of the method you're calling.

This is because (1) we would like to do at least some minimal type-checking on 
the method call, like whether 'self' can be passed as that argument, and (2) 
calling conventions can differ depending on the formal signature of the method, 
and we need to get that right for the call to succeed.

You could argue that in the specific case of an init method, we should guess 
about what this signature is;  but (1) there really are exceptions to the 
conventions, even with init methods, and (2) there's really no good reason for 
you to not have at least one declaration somewhere in the translation unit.

John.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Andy Lee
On Jan 22, 2013, at 1:26 PM, Jens Alfke j...@mooseyard.com wrote:
 
 On Jan 22, 2013, at 9:24 AM, Dave d...@looktowindward.com wrote:
 
 Ok, cool, thanks for that, I still have the same problem though when I call 
 the initWithManager.
 
 Others answered this already. To recap: The compiler needs to see a 
 declaration of an -initWithManager: method before it parses this line. So you 
 have to either #import the header that declares that method, or if there’s no 
 such header, put a category interface at the top of this .m file that defines 
 the method.

To spell it out even more... when the compiler sees this:

myObj = [[myClass alloc] initWithManager:self];

...it knows that [myClass alloc] returns an id. Since it doesn't have any hint 
as to the class of that id, the compiler will be permissive/optimistic/trusting 
and allow any message to be sent to it without warning -- *IF* it has seen a 
declaration of that message somewhere, anywhere, for any class. So you need to 
force the compiler to see such a declaration. Even importing a totally 
unrelated class will do.

As has been suggested, you could import a class that declares initWithManager:. 
Or you could add your own declaration of something else -- it could be a 
category, a protocol, *anything* -- that declares the method.

// This works (category).
@interface NSObject (AvoidCompilerWarning)
- (id)initWithArg:(id)arg;
@end

// Or this also works (protocol).
@protocol AvoidCompilerWarning
- (id)initWithArg:(id)arg;
@end

--Andy


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread Jens Alfke

On Jan 22, 2013, at 9:23 AM, Dave d...@looktowindward.com wrote:

 You could try declaring initWithManager: in a category on the class visible 
 only to your implementation code. (i.e. at the top of your .m file)
 
 The class name is passed in as a string and the class is formed from that, so 
 I can't pre-declare it. Please see my other reply.

Sure you can:

@interface SomeFictionalClassName
- (id) initWithManager: (Foo*)manager;
@end

Put that at the top of your source file and the warning will go away, because 
the compiler is now confident that there exists an -initWithManager: method. 
(The fact that you’ve declared it on a completely fictional class doesn’t 
matter.)

As others have said, though, it really sounds like what you want is a protocol 
that defines the -initWithManager: method. This is a better solution because it 
adds more type-safety — if you ever rename -initWithManager:, but don’t fix all 
the places it’s used, the compiler will then be able to complain about the 
places you missed, so you’ll avoid confusing runtime errors.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread Charles Srstka
On Jan 22, 2013, at 12:58 PM, Andy Lee ag...@mac.com wrote:

 // Or this also works (protocol).
 @protocol AvoidCompilerWarning
 - (id)initWithArg:(id)arg;
 @end

Really, a protocol is what you ought to be doing. Make a protocol with 
-initWithManager: in it, and then make all the classes that might get passed to 
this method comply with your protocol. Then, do this:

if ([myClass conformsToProtocol:@protocol(MyProtocol)])
myObj = [[myClass alloc] initWithManager:sel]];
else
myObj = [[myClass alloc] init];

The advantage to this method is a simple one: Suppose some random class happens 
to implement a method named -initWithManager:, but that method has nothing to 
do with your -initWithManager: other than a coincidental title, and takes a 
completely different type of object. Your original code will result in 
unpredictable behavior in this case (and probably throw an exception leading to 
a crash). If you use a protocol, you'll know not just that the method responds 
to something named -initWithManager:, but that it's *your* -initWithManager:

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Andy Lee
On Jan 22, 2013, at 2:19 PM, Charles Srstka cocoa...@charlessoft.com wrote:

 On Jan 22, 2013, at 12:58 PM, Andy Lee ag...@mac.com wrote:
 
 // Or this also works (protocol).
 @protocol AvoidCompilerWarning
 - (id)initWithArg:(id)arg;
 @end
 
 Really, a protocol is what you ought to be doing. Make a protocol with 
 -initWithManager: in it, and then make all the classes that might get passed 
 to this method comply with your protocol. Then, do this:
 
 if ([myClass conformsToProtocol:@protocol(MyProtocol)])
   myObj = [[myClass alloc] initWithManager:sel]];
 else
   myObj = [[myClass alloc] init];
 
 The advantage to this method is a simple one: Suppose some random class 
 happens to implement a method named -initWithManager:, but that method has 
 nothing to do with your -initWithManager: other than a coincidental title, 
 and takes a completely different type of object. Your original code will 
 result in unpredictable behavior in this case (and probably throw an 
 exception leading to a crash). If you use a protocol, you'll know not just 
 that the method responds to something named -initWithManager:, but that it's 
 *your* -initWithManager:

Makes sense, especially since it sounds like you have enough control of the 
class to declare it as conforming to the protocol.

To be extra fail-safe, you might want to perform a cast to be sure the right 
initWithManager: gets called:

if ([myClass conformsToProtocol:@protocol(MyProtocol)])
myObj = [(id MyProtocol)[myClass alloc] initWithManager:self];
else
myObj = [[myClass alloc] init];

--Andy

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Charles Srstka
On Jan 22, 2013, at 1:28 PM, Andy Lee ag...@mac.com wrote:

 On Jan 22, 2013, at 2:19 PM, Charles Srstka cocoa...@charlessoft.com wrote:
 
 On Jan 22, 2013, at 12:58 PM, Andy Lee ag...@mac.com wrote:
 
 // Or this also works (protocol).
 @protocol AvoidCompilerWarning
 - (id)initWithArg:(id)arg;
 @end
 
 Really, a protocol is what you ought to be doing. Make a protocol with 
 -initWithManager: in it, and then make all the classes that might get passed 
 to this method comply with your protocol. Then, do this:
 
 if ([myClass conformsToProtocol:@protocol(MyProtocol)])
  myObj = [[myClass alloc] initWithManager:sel]];
 else
  myObj = [[myClass alloc] init];
 
 The advantage to this method is a simple one: Suppose some random class 
 happens to implement a method named -initWithManager:, but that method has 
 nothing to do with your -initWithManager: other than a coincidental title, 
 and takes a completely different type of object. Your original code will 
 result in unpredictable behavior in this case (and probably throw an 
 exception leading to a crash). If you use a protocol, you'll know not just 
 that the method responds to something named -initWithManager:, but that it's 
 *your* -initWithManager:
 
 Makes sense, especially since it sounds like you have enough control of the 
 class to declare it as conforming to the protocol.
 
 To be extra fail-safe, you might want to perform a cast to be sure the right 
 initWithManager: gets called:
 
 if ([myClass conformsToProtocol:@protocol(MyProtocol)])
   myObj = [(id MyProtocol)[myClass alloc] initWithManager:self];
 else
   myObj = [[myClass alloc] init];
 
 --Andy

That's a good fix. In addition to being more fail-safe, that also lets clang 
and Xcode know exactly what you intend here, so that if you decide someday to 
do something like rename the initWithManager: method using Xcode's Refactor 
feature, it should get this invocation of the method as well.

Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-22 Thread Jens Alfke

On Jan 22, 2013, at 10:15 AM, Dave d...@looktowindward.com wrote:

 This has to work with classes that exist already as well as classes that 
 don't. If initWithManager is defined in the class in question knows what it 
 is being called like this, if not then it defaults to the regular NSObject 
 init.
 
 For instance it could be NSString or NSArray that is the class, in which case 
 it won't have initWithManager so it calls init instead.

That will still work fine with protocols. NSString doesn’t implement your 
protocol, so your code skips the custom init and just calls -init instead.

If you don’t believe us, then try typing in and compiling the examples people 
have given, and experiment with them until you’re convinced. But this thread is 
kind of going around in circles, with you asking us for advice and then not 
accepting it.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to avoid warning?

2013-01-22 Thread Dave


On 22 Jan 2013, at 21:27, Jens Alfke wrote:



On Jan 22, 2013, at 10:15 AM, Dave d...@looktowindward.com wrote:

This has to work with classes that exist already as well as  
classes that don't. If initWithManager is defined in the class in  
question knows what it is being called like this, if not then it  
defaults to the regular NSObject init.


For instance it could be NSString or NSArray that is the class, in  
which case it won't have initWithManager so it calls init instead.


That will still work fine with protocols. NSString doesn’t  
implement your protocol, so your code skips the custom init and  
just calls -init instead.


If you don’t believe us, then try typing in and compiling the  
examples people have given, and experiment with them until you’re  
convinced. But this thread is kind of going around in circles, with  
you asking us for advice and then not accepting it.




I just wanted rid of the warning which I have, so I'm happy. I tried  
with the protocols etc. but it makes the method very messy and I have  
to double up on local variables etc. The fact is that it's a one  
parameter method that accepts an id and returns an id. If someone did  
define initWithManager on a class then it's likely to match the  
method pattern anyway, so it probably wouldn't help stop problems  
like that occurring anyway, and I'm not defining the classes that  
have the initWithManager method in them, so I can't define the  
class as conforming to a protocol.


Bottom line, it works as it is and it isn't that fragile, so it ain't  
broke and ain't likely to break then leave it alone is my motto.


Thanks a lot for all your help
All the Best
Dave


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

How to avoid warning?

2013-01-21 Thread Dave

Hi All,

I have the following code:

if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)
myObj = [[myClass alloc] init];
else
myObj = [[myClass alloc] initWithManager:sel]];


I get a warning on the initWithManager: statement (Obviously), how to  
avoid the warning or otherwise fix it?


Thanks in Advance
Dave

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-21 Thread David Duncan
On Jan 21, 2013, at 10:14 AM, Dave d...@looktowindward.com wrote:

 Hi All,
 
 I have the following code:
 
 if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)
   myObj = [[myClass alloc] init];
 else
   myObj = [[myClass alloc] initWithManager:sel]];
 
 
 I get a warning on the initWithManager: statement (Obviously), how to avoid 
 the warning or otherwise fix it?


What warning do you get? (Its not obvious from context).
--
David Duncan


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-21 Thread Tom Davie
On 21 Jan 2013, at 18:14, Dave d...@looktowindward.com wrote:

 Hi All,
 
 I have the following code:
 
 if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)
   myObj = [[myClass alloc] init];
 else
   myObj = [[myClass alloc] initWithManager:sel]];
 
 
 I get a warning on the initWithManager: statement (Obviously), how to avoid 
 the warning or otherwise fix it?

Simple,

Write better code where you know the types you're dealing with ;).

Bob
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-21 Thread Robert Martin
 myObj = [[myClass alloc] initWithManager:sel]];

Is sel meant to be self?

On Jan 21, 2013, at 1:14 PM, Dave d...@looktowindward.com wrote:

 Hi All,
 
 I have the following code:
 
 if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)
   myObj = [[myClass alloc] init];
 else
   myObj = [[myClass alloc] initWithManager:sel]];
 
 
 I get a warning on the initWithManager: statement (Obviously), how to avoid 
 the warning or otherwise fix it?


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-21 Thread Quincey Morris
On Jan 21, 2013, at 10:14 , Dave d...@looktowindward.com wrote:

   myObj = [[myClass alloc] initWithManager:sel]];
 
 
 I get a warning on the initWithManager: statement (Obviously), how to avoid 
 the warning or otherwise fix it?

You need to #import a header file with an @interface declaration for the 
'initWithManager:' method.

The rule is that when the compiler sees a message send to a receiver of type 
'id' (which is the return type of 'alloc'), it needs to have seen *some* 
declaration of that method. If it has seen more than one declaration, BTW, the 
declarations must all be compatible in terms of parameter and return types.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-21 Thread Steve Sisak

At 6:14 PM + 1/21/13, Dave wrote:

if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)
myObj = [[myClass alloc] init];
else
myObj = [[myClass alloc] initWithManager:sel]];

I get a warning on the initWithManager: statement (Obviously), how 
to avoid the warning or otherwise fix it?


You could try declaring initWithManager: in a category on the class 
visible only to your implementation code. (i.e. at the top of your .m 
file)


HTH,

-Steve
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: How to avoid warning?

2013-01-21 Thread Jens Alfke

On Jan 21, 2013, at 10:14 AM, Dave d...@looktowindward.com wrote:

 if (class_RespondsToSelector(myClass,@selector(initWithManager:) == NO)

Off-topic: instead of using the Obj-C runtime’s C API, you can express this as
if ([myClass instancesRespondToSelector: @selector(initWithManager:)] 
== NO)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com