开发者

Subclassing NSMutableString

I know the first thing to do when one has a question with Obj-C is to read the Apple docs. And I did. I read p.95-p.102 of the Cocoa Fundamentals Guide. So this is what I know about composite object creation from class clusters.

  • Subclassing of class clusters should be done if provided functions need to be modified.
  • Composite objects must override primitive methods of the super class. (And I also read that the primitive methods of the super class of the super class have to be overridden as well.)

So I applied what I know. Here's my code...

#import <Foundation/Foundation.h>

@interface GOObjectKeyName : NSMutableString
{
    NSMutableString *embeddedNSMutableString;
}

//开发者_开发百科+ GOObjectKeyName;

//- init;

//From NSString
- (NSUInteger) length;

// From NSString
- (unichar) characterAtIndex: (NSUInteger) index;

// From NSString
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange;

// From NSMutableString
- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString;

//- (void) dealloc;

@end



#import "GOObjectKeyName.h"

@implementation GOObjectKeyName

/*
+ GOObjectKeyName
{
    return [[[self alloc] init] autorelease];
}

- init
{
    self = [super init];
    if (self)
    {
        embeddedNSMutableString = [[NSMutableString allocWithZone: [self zone]] init];
    }
    return self;
}
*/

- (NSUInteger) length
{
    return [embeddedNSMutableString length];
}

- (unichar) characterAtIndex: (NSUInteger) index
{
    return [embeddedNSMutableString characterAtIndex: index];
}

- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
{
    [embeddedNSMutableString getCharacters: buffer range: aRange];
}


- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString
{
    [embeddedNSMutableString replaceCharactersInRange: aRange withString: aString];
}

/*
- (void) dealloc
{
    [embeddedNSMutableString release];
    [super dealloc];
}
*/

@end

The commented methods are commented because I wasn't sure if they were necessary. I just wrote them down because I saw them in the example. And you also might see that it's basically another NSMutableString class. But don't worry, there is a bigger purpose to it, I just want to know that the basics are correct before I continue since this is my first time with composite objects. And as you know... IT DOESN'T WORK!! Please understand my frustration after hours of trial and error. If someone could guide me here it would be a great relief.

Oh, and if you don't mind I have another small question. The example on the apple docs describe the NSNumber analogy. But is NSNumber actually more efficient then using int, char, and other fundamental types? Just curious.


It would certainly help if you would characterize the failure beyond "it doesn't work."

However, your -init method is commented out, so there's no way that embeddedMutableString is going to get created. And even if you uncomment it, you call +alloc to create embeddedMutableString, but you never initialize it.

After looking at the documentation, it seems that a subclass's -init method should look like this (warning: untested code):

-(id)init
{
    if (self = [super init]) {
        embeddedMutableString = [[NSMutableString alloc] init];
    }
    return self;
}

You will, of course, also have to provide a -dealloc that releases embeddedMutableString and calls [super dealloc].

As far as NSNumber, no, it's not more efficient than basic types like int but it does have other advantages. NSNumber is a class, so you can store instances of NSNumber in Cocoa's collection classes such as NSArray and NSSet. You can write them to property lists. NSDecimalNumber avoids the imprecision that comes from converting between decimal and binary notations. And so on...


To answer your NSNumber question, no, NSNumber is not more efficient than the C primitives. NSNumber is an encapsulation for the C primitives, essentialy allowing primitives to be inserted into objects like NSArray and NSSet. If you need that encapsulation ability, but want the speed of an actual int or float, it's simple enough to write your own NSNumber-like class that works for just one type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜