Re: instance fails in its own class
Re: instance fails in its own class
- Subject: Re: instance fails in its own class
- From: N!K <email@hidden>
- Date: Sun, 7 Nov 2010 20:23:59 -0800
Success! Your last sentence pointed the way.
On Nov 6, 2010, at 11:25 PM, Greg Guerin wrote:
> N!K wrote:
>
>> However, exactly the same statement fails when pasted into -init of Class.m.
>>
>> Build yields "warnings Class may not respond to -new."
>
>
> This message suggests you're calling an instance method, not a class method, or that's the way the compiler is interpreting it.
Yes. I had completely missed the association between +new, which includes -init, and my explicit -(id)init, which confused the compiler . My -init of course overrides all other inits including those with assignments. This last point I knew well.
> Post your actual failing code, in the complete method where it fails.
That was a mess from all the errors in troubleshooting, so I'm not including it here. An interesting but frustrating round was the chase of misleading compiler errors when my small block of code worked in InstanceAppDelegate as long as Untitled was empty, but got warnings in both places as soon as I put anything into Untitled. Instance ran but showed more errors and stalled.
However, in order to not leave you hanging, here is what I wound up with, which does work:
#import "Untitled.h"
@implementation Untitled
-(void)alphaObj
{
Untitled *objk;
objk = [Untitled new];
NSLog(@"objk = %@",objk);
//return self;
}
@end
I also added [obj alphaObj]; to InstanceAppDelegate.m to call it.
I had used -(id)init with my code in it instead of -(void)alphaObj because it calls itself, which provides some separation and is a convenience while concentrating on learning some exercises.
> For example, is your class really named Class? If so, then that won't work. The reason is that Class is already a defined type, and it doesn't descend from NSObject. See the return type of the NSObject method -class. Look it up in "The Objective-C Programming Language" reference doc, under the heading "Defined Types", or see the include file "objc/objc.h".
Thanks, I had previously found Class (capitalized) used as the name of a class in an Apple guide example, which contrasted it with class (small initial) which is defined, as in @class. To be safe, I'll avoid anything close. I was trying to use Class in my email in order to be clear, and it backfired. Sorry.
>
> Furthermore, since +new is defined as +alloc followed by -init,
This was meant to initialize Class
> calling +new in -init
while this was meant to initialize the new object. I had no idea of redundancy here.
> seems a little recursive to me, but without seeing actual code, it's just a guess.
>
> -- GG
What a lot of trouble for such a small item! I'm amazed at the steep learning curve in Objective C, Cocoa and Xcode on my own. After exhausting all my own thoughts and Google with many trials, I really appreciate having you and this group to turn to.
Nick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden