• 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: Noob question regarding Cocoa objects.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Noob question regarding Cocoa objects.


  • Subject: Re: Noob question regarding Cocoa objects.
  • From: James Bucanek <email@hidden>
  • Date: Wed, 29 Nov 2006 09:16:39 -0700

Nir Soffer wrote on Tuesday, November 28, 2006:

>
>On Nov 28, 2006, at 18:42, James Bucanek wrote:
>
>> You can also call another init method in the same class, but in the
>> end the appropriate super class' init method must be called first
>> before you start initializing your object
>>
>>     - (id)initWithParam:(int)param
>>     {
>>         if ( (self=[self init]) != nil )
>>             {
>>             // [self init] did basic initialization
>>             // do more initialization with param here
>>             iVar = param;
>>             ...
>
>I guess you mean:
>
>     - (id)initWithParam:(int)param
>     {
>         if ( (self=[super init]) != nil )
>             {
>             // [super init] did basic initialization
>             // do more initialization with param here
>             iVar = param;
>             ...

No, I wrote exactly what I meant. I was giving cookbook examples of how to construct objects in Objective-C that mimic familiar Java paradigms. Specifically, this example draws an analogy to Java's syntax for chaining constructors in the same class:

public class MyClass {

    private int     iCount;
    private String  iVar;

    public MyClass( )
    {
        iCount = 1;
    }

    public MyClass( String param )
    {
        this();
        iVar = param;
    }

In Java, the special statement 'this()' in the MyClass(String) constructor causes the MyClass object to be initialized by first calling the MyClass() constructor. Execution then continues with the MyClass(String) constructor. In this example, using 'new MyClass("Hello")' to create the object results in iCount=1 and iVar="Hello".

If you're not a Java programmer, you probably don't worry about these things. ;)

--
James Bucanek
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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

References: 
 >Re: Noob question regarding Cocoa objects. (From: Nir Soffer <email@hidden>)

  • Prev by Date: Re: Modifying the dragged image during a drag / drop operation
  • Next by Date: Re: Modifying the dragged image during a drag / drop operation
  • Previous by thread: Re: Noob question regarding Cocoa objects.
  • Next by thread: memory allocation and virtual memory increase
  • Index(es):
    • Date
    • Thread