• 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
Re: NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSData


  • Subject: Re: NSData
  • From: matt neuburg <email@hidden>
  • Date: Thu, 12 Dec 2002 22:33:46 -0800

On Thu, 12 Dec 2002 17:36:02 -0800 (PST), email@hidden said:

>
>So basically what you tell me is this:
>
>---------------------------
>@interface MyDataObject : NSObject {
> NSData * mData;
> -- All added members here --
>}
>--- all added methods here ---
>@end
>---------------------------

Yes, this is called a "wrapper". There are two kinds of relationships in object-oriented programming, "is-a" (inheritance) and "has-a" (wrapper). Each has its place.

>Consequence of what, if I want for instance to access
>some data chunk inside of my data, I would have to do
>for instance:
>
>---------------------------
>@implementation MyDataObject
>--- among other methods: ---
>
>- (NSData *)subdataWithRange:(NSRange)range {
> return [mData subdataWithRange:range];
>}
>
>--- etc ---
>
>@end
>---------------------------
>
>or something like this.
>This is apparently very weird, because I have then to
>reimplement likewise all the methods that I would get
>"for free" by inheritance. At least, that's what I would
>get in C++

Welcome to Objective-C. It ain't C++ (thank heavens!). In this world, you do NOT have to reimplement them all. If you get a message you don't respond to, you just pass it along to the wrapped object.

The following code demonstrates. Try it on your own machine. You will be amazed...!

@interface MyString : NSObject {
NSString* theString; // string wrapper
}
- (NSString*) test;
@end

@implementation MyString

- (id) init {
self = [super init];
self->theString = [@"howdy" retain];
return self;
}

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

- (NSString*) test {
return @"It works.";
}

- (void)forwardInvocation:(NSInvocation *)invocation {
if ([self->theString respondsToSelector:[invocation selector]])
[invocation invokeWithTarget:self->theString];
else
[self doesNotRecognizeSelector:[invocation selector]];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
if ([theString respondsToSelector: aSelector])
return [theString methodSignatureForSelector:aSelector];
return [super methodSignatureForSelector:aSelector];
}

@end

And here is some code to test it with:

MyString* s = [[MyString alloc] init];
// this works, because MyString responds to "test"
NSRunAlertPanel(@"Test1", [s test], nil, nil, nil);
// this works even though MyString does not respond to "uppercaseString"
// !!!! that's because it is automatically passed along...
// to the wrapped NSString
NSRunAlertPanel(@"Test2", [s uppercaseString], nil, nil, nil);
// this fails, correctly - neither MyString nor NSString can respond
NSRunAlertPanel(@"Test3", [s junk], nil, nil, nil);
[s release];

m.



matt neuburg, phd = email@hidden, http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: NSTableView oddity
  • Next by Date: Re: 2 in one
  • Previous by thread: Re: NSData
  • Next by thread: Re: NSData
  • Index(es):
    • Date
    • Thread