• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Need Help With Creating NSMutableNumber Class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Need Help With Creating NSMutableNumber Class


  • Subject: Need Help With Creating NSMutableNumber Class
  • From: Chase <email@hidden>
  • Date: Sun, 10 Jul 2005 23:14:38 -0500

I'm trying to build a class to function as the NSMutableNumber class would if it existed.

NSNumber is a cluster class, so subclassing it directly wasn't working. I decided to make my own little aggregate class ("XXXMutableNumber") that contains an instance variable ("_number") which holds the actual NSNumber. I understand that there are a few different ways that I could have tackled this, but, please just help me with this particular method that I've already chosen.

The setters and getters and alloc/init pairs are all working great, but I'm not exactly sure how I should write the class method counterparts to the "-initWithBlaBlaBla:" methods ("+numberWithBlaBlaBla:" for example).

Please (1) help me with the "+numberWithBlaBlaBla:" methods, and (2) look at what I've got below and tell me if I'm getting any of the memory management stuff wrong:



    #import <Foundation/Foundation.h>

    @interface XXXMutableNumber : NSObject {
        NSNumber * _number;
    }
    - (void)setBoolValue:(BOOL)value;
    // setCharValue, setDoubleValue, etc...
    @end



    @implementation XXXMutableNumber

    - (id)initWithBool:(BOOL)value {
        self = [super init];
        if (self) {
            _number = [[NSNumber alloc] initWithBool:value];
        }
        return self;
    }

    // initWithChar, initWithDouble, etc...


* (XXXMutableNumber)numberWithBool:(BOOL)value { // THIS IS THE MAIN THING I NEED HELP WITH
// ??????????
}


    // numberWithChar, numberWithDouble, etc...


- (void)setBoolValue:(BOOL)value { [_number release]; _number = [[NSNumber alloc] initWithBool:value]; }

    // setCharValue, setDoubleValue, etc...


- (BOOL)boolValue { return [_number boolValue]; }

    // charValue, doubleValue, etc...


@end


NOTE: If you're reading this in the archives a hundred years from now, please read through the entire thread to make sure any bugs are corrected before you copy/paste into your own project.


Thanks.

- Chase




_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Need Help With Creating NSMutableNumber Class
      • From: Chase <email@hidden>
  • Prev by Date: Re: Why does this leak memory?
  • Next by Date: Re: Documentation frustrations
  • Previous by thread: Re: Simple C File IO Question (with code listing)
  • Next by thread: Re: Need Help With Creating NSMutableNumber Class
  • Index(es):
    • Date
    • Thread