new class

  • HELP! this is driving me nuts.

    In a new class (subclass of NSObject) I have the following method:

    - (DCPoint)convert {                    // gives this error: can not use an object
    as parameter to a method
    DCPoint * DC = [[DCPoint alloc] init];

    // insert calculations here

    [DC initWithDistance:0 Course:0];
    return DC;      // gives this error: incompatible types in return

    }

    my method does not have any parameters, which is why that first error
    is driving me nuts. what does it mean?

    there was also a warning: control reaches end of non-void function,
    but that disappeared somehow. strange...

    --
    GPG Key ID: 92AD877F7E4E32B9
    Fingerprint: 16B0 7DCD 3AB0 0C45 F5FD  581D 92AD 877F 7E4E 32B9
  • On 20/09/2007, H M <hmiersch...> wrote:
    > HELP! this is driving me nuts.
    >
    > In a new class (subclass of NSObject) I have the following method:
    >
    > - (DCPoint)convert {                                    // gives this error: can not use an object

    -(DCPoint*)convert

    Note the asterisk!

    --
    Igor M.
  • > - (DCPoint)convert {                    // gives this error: can not use an object
    > as parameter to a method
    >

      You seem to be missing some basics and should really review this
    document thoroughly:
      http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.
    html


    --
    I.S.
  • > In a new class (subclass of NSObject) I have the following method:
    >
    > - (DCPoint)convert {                    // gives this error: can not use an object
    > as parameter to a method
    > DCPoint * DC = [[DCPoint alloc] init];
    >
    > // insert calculations here
    >
    > [DC initWithDistance:0 Course:0];
    > return DC;                         // gives this error: incompatible types in return
    >
    > }

    Also note that you init DC twice, but alloc it only once.  In
    general, init should always be used concertedly with alloc. The line
    before the return should most likely be divided into two lines where
    you use setters to change the variables of DC:

    [DC setDistance:0];
    [DC setCourse:0];

    Cheers, Patrick
  • On 9/20/07, PGM <meirmans...> wrote:
    >> In a new class (subclass of NSObject) I have the following method:
    >>
    >> - (DCPoint)convert {                                  // gives this error: can not use an object
    >> as parameter to a method
    >> DCPoint * DC = [[DCPoint alloc] init];
    >>
    >> // insert calculations here
    >>
    >> [DC initWithDistance:0 Course:0];
    >> return DC;                                              // gives this error: incompatible types in return
    >>
    >> }
    >
    > Also note that you init DC twice, but alloc it only once.  In
    > general, init should always be used concertedly with alloc. The line
    > before the return should most likely be divided into two lines where
    > you use setters to change the variables of DC:
    >
    > [DC setDistance:0];
    > [DC setCourse:0];

    ...and you are leaking the DCPoint instance that gets allocated (not
    following the Cocoa memory "contract").

    -Shawn
  • > ...and you are leaking the DCPoint instance that gets allocated (not
    > following the Cocoa memory "contract").

    the problem is that I can't wrap my head around that memory
    management stuff. I never know when to retain an object and when not
    to retain it.
  • <
    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concep
    ts/ObjectOwnership.html#//apple_ref/doc/uid/20000043

    >
    This is a must read and a must-perfectly-understand before doing anything
    with Cocoa.

    --
    Julien

    On 9/21/07, H M <hmiersch...> wrote:
    >
    >> ...and you are leaking the DCPoint instance that gets allocated (not
    >> following the Cocoa memory "contract").
    >
    > the problem is that I can't wrap my head around that memory
    > management stuff. I never know when to retain an object and when not
    > to retain it.
    >
  • Hi,

    You retain an object when you want to have reference to it later. For
    example, to set an ivar you do something like this:

    - (void)setString:(NSString*)string
    {
      [_string release];
      _string = [string retain];
    }

    Hope this helps and isn't confusing.

    Peace, Alan

    --
    My blog: cocoalatte.info

    // Things I've said -------------------------
    "Maturity resides in the mind."
    "Silence is the Universe's greatest gift."
    "When the World realizes that personal beliefs are not something to
    argue or fight over, it shall evolve."
  • You retain an object when you want to have reference to it later. For
    example, to set an ivar you do something like this:

    - (void)setString:(NSString*)string
    {
      [_string release];
      _string = [string retain];
    }

      Please don't re-state Cocoa's memory management conventions unless you are prepared to do it correctly.  It is much more efficient to simply provide a link to Apple's documentation and avoid littering this forum with bad or incorrect advice such as your broken sample code.

      Simply stated Cocoa Memory Management Rules
      http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/
    MemoryManagementRules.html


      Introduction to Memory Management (not Cocoa specific):
      http://developer.apple.com/documentation/mac/Memory/Memory-9.html

      Basic Cocoa Accessor Methods:
      http://developer.apple.com/documentation/Cocoa/Conceptual/ModelObjects/Arti
    cles/moAccessorMethods.html#//apple_ref/doc/uid/TP40002132-SW1

      http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals
    /AddingBehaviortoaCocoaProgram/chapter_4_section_5.html#//apple_ref/doc/uid
    /TP40002974-CH5-SW5


      Search for more...
  • On Sep 21, 2007, at 4:53 AM, Alan Smith wrote:

    > Hi,
    >
    > You retain an object when you want to have reference to it later. For
    > example, to set an ivar you do something like this:
    >
    > - (void)setString:(NSString*)string
    > {
    > [_string release];
    > _string = [string retain];
    > }
    >
    > Hope this helps and isn't confusing.

    All fine and good unless string and _string are the same object. :)

    Review...

    <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
    Articles/mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539
    >

    The "zero" rule of Cocoa memory management.... the rules are easy
    believing the rules are that easy is the hard part.

    -Shawn
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today