Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Custom class method problem



Shawn,

Thanks for the reply.  Now a follow-up...

On Sep 2, 2005, at 6:04 PM, Shawn Erickson wrote:


On Sep 2, 2005, at 2:48 PM, Phil - Pub wrote:


I've run out of ideas on what I'm doing wrong here..


This isn't a Objective-C language question, in the future use the Cocoa-dev list found on the same site you found this list.



I debated sending to cocoa-dev but the answers provided seem to indicate that this was the right choice even though this problem involves Cocoa, the root issue seems to be my understanding of what Objective C needs/is doing. Please read on...



test.h:
@interface myObject : NSObject {
    NSMutableDictionary *attributeList;
}

- (void)setAttributeName:(NSString *)attrValue forKey:(NSString *) attrKey;

@end

test.m:
@implementation myObject


-(void)init { attributeList = [[NSMutableDictionary alloc] init]; }

- (void)dealloc {
    [attributeList release];
    [super dealloc];
}


It was my (mis?)understanding that initialization of class objects would happen automatically when I call [[myObject alloc] init]. Your information tells me I need to handle object allocation in the class myself which is fine if that's what needs to be done... just trying to understand what needs to happen.


However, I copied and pasted this snippet and it caused some compiler warnings:
/Users/phil/Documents/Development/Mac OS X/Projects/Test - Cocoa App/ test.m:19: warning: multiple methods named '-init' found
/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h: 72: warning: using '-(id)init'
/Users/phil/Documents/Development/Mac OS X/Projects/Test - Cocoa App/ test.m:18: warning: also found '-(void)init'


as well as runtime errors:
2005-09-02 18:13:33.241 Test - Cocoa App[2287] *** -[NSCFDictionary setAttributeName:forKey:]: selector not recognized [self = 0x306c10]
2005-09-02 18:13:33.241 Test - Cocoa App[2287] *** Uncaught exception: <NSInvalidArgumentException> *** -[NSCFDictionary setAttributeName:forKey:]: selector not recognized [self = 0x306c10]



-(void)setAttributeName:(NSString *)attrValue forKey:(NSString *) attrKey {
[attributeList setValue:attrValue forKey:attrKey];
}


@end

main.m:
char *tak1 = "TestAttr key 1";
char *tak2 = "TestAttr key 2";
char *tav1 = "Attr 1 value";
char *tav2 = "Attr 2 value";
myObj = [myObject new];


In general "new" is considered a legacy method, instead use [[myObject alloc] init].



Good to know... thanks.


[myObj setAttributeName:[NSString stringWithCString: tav1] forKey: [NSString stringWithCString: tak1]];

or more simply...
[myObj setAttributeName:@"Attr 1 value" forkey:@"TestAttr key 1"];


[myObj setAttributeName:[NSString stringWithCString: tav2] forKey: [NSString stringWithCString: tak2]];

or  more simply...
[myObj setAttributeName:@"Attr 2 value" forkey:@"TestAttr key 2"];


This part I knew... I need the C string syntax as, once this is working, the parameters will actually be passed from C code.


[myObj release]; //when done with myObj

---

Now try it.

-Shawn



Thanks, Phil

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden
References: 
 >Custom class method problem (From: Phil - Pub <email@hidden>)
 >Re: Custom class method problem (From: Shawn Erickson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.