• 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: functions vs methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: functions vs methods


  • Subject: Re: functions vs methods
  • From: Ricky Sharp <email@hidden>
  • Date: Thu, 05 May 2005 08:34:14 -0500

On Thursday, May 05, 2005, at 08:24AM, Roland Silver <email@hidden> wrote:

>Given one of my classes, Foo, how can I define a function (not method)
>post() which accesses an instance variable textView of Foo? The
>following doesn't work:
>
>//	Foo.h
>#import <Cocoa/Cocoa.h>
>#import <stdio.h>
>@interface Foo : NSObject
>{
>     IBOutlet NSTextView *textView;
>}
>... method declarations ...
>
>void post(const char *template, ...);
>@end
>
>//	Foo.m
>@implementation Foo
>... method definitions ...
>
>void post(const char *template, ...) {
>	va_list	args;
>	char	*msg;
>	NSString *nsMsg;
>	va_start(args, template);
>	vasprintf(&msg, template, args);
>	va_end(args);
>	nsMsg = [NSString stringWithCString:msg];
>	[textView insertText:nsMsg]; //<------- error: 'textView' undeclared
>} //end post
>@end

I would rewrite your C function to just return the string.  The various calling methods could then use the string as needed.  Furthermore, I'd move the function to just another module (i.e. not live in your class' implementation).

--
Rick Sharp
Instant Interactive(tm)

 _______________________________________________
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

References: 
 >functions vs methods (From: Roland Silver <email@hidden>)

  • Prev by Date: functions vs methods
  • Next by Date: Re: Setting the contents of an NSTokenField programatically
  • Previous by thread: functions vs methods
  • Next by thread: Re: functions vs methods
  • Index(es):
    • Date
    • Thread