Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / November 2007



Tip: Looking for answers? Try searching our database.

Class method and autorelease objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick Tamburri - 15 Nov 2007 15:25 GMT
I want to set up some user defaults in an application, but I keep
getting an address check when I go to regirster the defaults.  This is
not the first time I have registered user defaults, but it is the
first time I tried to do it using autorelease objects returned from a
class method.

I have a class that looks like this (simplified):

*** MyKeyCode.h
@interface MyKeyCode : NSObject {
 unichar key;
 unsigned int modifiers;
}

- (id) initWithCommandKey:(unichar) newKey andModifier:(unsigned int) newModifiers;
+ (MyKeyCode *) newKeyWithCommandKey:(unichar) newKey andModifier:(unsigned int) newModifiers;
@end

*** MyKeyCode.m
- (id) initWithCommandKey:(unichar) newKey andModifier:(unsigned int) newModifiers
{
   if (self = [super init]) {
       key = newKey;
       modifiers = newModifiers;
   }
   return self;
}

+ (MyKeyCode *) newKeyWithCommandKey:(unichar) newKey andModifier:(unsigned int) newModifiers
{
 return [[[MyKeyCode alloc] initWithCommandKey:newKey andModifier:newModifiers] autorelease;
        //^^^^^^^^^^^^^^^^^ The problem?
}

*** AppController.m

NSString *NCTSpaceKey = @"MySpaceKey";

+ (initialize)
 NSMutableDictionaty *defaults = [NSMutableDictionary dictionary];

 ...

 [defaults setObject:[MyKeyCode newKeyWithCommandKey:' ' andModifier:0]
       forKey:NCTSpaceKey];

 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];  // << ADDRESS CHECK

 ...

===============
I think I am generating or returning the object to be stored
incorrectly, but I have not been able to figure out my error, and
neither Google not the Xcode docs have been useful in this instance.

Any pointers would be appreciated.  Thank you.

                                               /nt
Signature

Delete this to reply directly.

David Phillip Oster - 15 Nov 2007 15:35 GMT
> I want to set up some user defaults in an application, but I keep
> getting an address check when I go to regirster the defaults.  This is
[quoted text clipped - 32 lines]
>   return [[[MyKeyCode alloc] initWithCommandKey:newKey
>   andModifier:newModifiers] autorelease;
missing close ']'
>          //^^^^^^^^^^^^^^^^^ The problem?
> }
[quoted text clipped - 13 lines]
>   [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];  // <<
>   ADDRESS CHECK

You are putting an object into UserDefaults that UserDefaults can't handle.
See

file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Conceptual/UserDefaults

which says:

Note that a default's value can be only property list objects:
NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

That document goes on to describe how you deal with storing other
information in UserDefaults, using NSColor as an example.
Nick Tamburri - 15 Nov 2007 19:37 GMT
> You are putting an object into UserDefaults that UserDefaults can't handle.
> See
[quoted text clipped - 8 lines]
> That document goes on to describe how you deal with storing other
> information in UserDefaults, using NSColor as an example.

I see.

I read about NSColor's limitation in Hillegass' book, but I was lead
to believe that the "problem" was with NSColor itself, not a
limitation of NSUserDefaults.

Thanks.

                                               /nt
Signature

Delete this to email directly.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.