• 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: Simple question - Subclassing NSView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple question - Subclassing NSView


  • Subject: Re: Simple question - Subclassing NSView
  • From: "Hamish Allan" <email@hidden>
  • Date: Thu, 12 Jun 2008 18:33:05 +0100

Hi Vikas,

On Thu, Jun 12, 2008 at 6:26 PM, Vikas <email@hidden> wrote:

>  I have recently started programming on Mac using Objective-C and Cocoa. I am coming from C++/C# world. So, its a fairly basic question. Please help me understand the following code:
>
>  @implementation MyView     /*MyView inherits from NSView */
>  -(void)drawRect: (NSRect)aRect {
>                  [[NSColor blackColor] set];
>                  NSRectFill( [self bounds] );
>  }
>
>  In first line, I was expecting something like [self setColor:[NSColor blackColor]];   (similar to this.color = NSColor.blackColor; in C#/C++)
>  how NSColor object knows about where to set the color?

It's not setting the color of the NSView, it's setting the color of
the pen used by subsequent drawing operations such as NSRectFill().

>  In second line, NSRectFill(), I was expecting it to be called using square bracket [] notation. Again how this function knows where to fill the rectangle? There is no reference of NSView passed into the function?

The NSView reference is "self", as the code is implementing a method
of an NSView subclass. NSRectFill is a C function, not an Objective-C
method; you can mix C and Objective-C freely in a .m file.

>  Lastly what are the rules of using () verses []?

If you want to call a plain C function, use the function name plus
parentheses just as you would in a plain C program. If you want to
call a method on an object, use the square bracket notation [objName
methodSelector].

Hamish
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Simple question - Subclassing NSView
      • From: Vikas <email@hidden>
References: 
 >Simple question - Subclassing NSView (From: Vikas <email@hidden>)

  • Prev by Date: Re: Accessors Question
  • Next by Date: Re: Authentication and NSURLConnection sendSynchronousRequest
  • Previous by thread: Re: Simple question - Subclassing NSView
  • Next by thread: Re: Simple question - Subclassing NSView
  • Index(es):
    • Date
    • Thread