Sharing code in a project

  • This is probably a fairly basic question (I hope), but I want to make
    sure that I'm going about this the proper way.

    With some older C/C++ applications I wrote years ago, if there was a
    set of commonly used functions, I would throw them into a .h file so
    I could easily reference those functions from pretty much anywhere
    inside the application.

    What would be the best way to do a similar thing in a Cocoa
    application?  Just writing a .h file and throw in the functions?  Or
    would I have to wrap a class around each function?  Write a
    framework, perhaps?  Or is there some other way about doing things
    that I'm just entirely missing?

    Ultimately, I'd like to be able to write some code similar to this in
    my code which would refer to the function/method in the commonly
    shared file.

    [self myMethod: someVariable];

    Chad
  • On Oct 24, 2006, at 7:52 PM, Chad Armstrong wrote:

    > What would be the best way to do a similar thing in a Cocoa
    > application?  Just writing a .h file and throw in the functions?

    It's a pretty broad question. If there are just a handful of straight
    C functions, then that would be fine. If there's a lot (like dozens),
    you might want to group them somehow.

    > Ultimately, I'd like to be able to write some code similar to this
    > in my code which would refer to the function/method in the commonly
    > shared file.
    >
    > [self myMethod: someVariable];

    This is a different because the method would belong to a particular
    class or category.

        - Scott
  • On Oct 24, 2006, at 10:52 PM, Chad Armstrong wrote:

    > This is probably a fairly basic question (I hope), but I want to
    > make sure that I'm going about this the proper way.
    >
    > With some older C/C++ applications I wrote years ago, if there was
    > a set of commonly used functions, I would throw them into a .h file
    > so I could easily reference those functions from pretty much
    > anywhere inside the application.
    >
    > What would be the best way to do a similar thing in a Cocoa
    > application?  Just writing a .h file and throw in the functions?
    > Or would I have to wrap a class around each function?  Write a
    > framework, perhaps?  Or is there some other way about doing things
    > that I'm just entirely missing?

    It really depends on what you are trying to do.  One way is to create
    a bunch of static methods on a Utility class.  Then you can call it
    from anywhere.  e.g.  Utility.prettyPrintMyString("This is a test."),
    etc...  However, if you find that you are writing a lot of these
    static methods, you probably don't have a good object model design.
    You should try to put these methods where they belong according to
    which class has responsibility for that functionality.
  • On Oct 24, 2006, at 11:29 PM, Chad Armstrong wrote:

    >>
    >> It really depends on what you are trying to do.  One way is to
    >> create a bunch of static methods on a Utility class.  Then you can
    >> call it from anywhere.  e.g.  Utility.prettyPrintMyString("This is
    >> a test."), etc...  However, if you find that you are writing a
    >> lot of these static methods, you probably don't have a good object
    >> model design.  You should try to put these methods where they
    >> belong according to which class has responsibility for that
    >> functionality.

    Sorry, I gave you a Java example.  Of course, the same applies to
    ObjC.  [Utility prettyPrintMyString:@"This is a test"];
  • On Oct 24, 2006, at  10:37 PM, Robert La Ferla wrote:
    > Sorry, I gave you a Java example.  Of course, the same applies to
    > ObjC.  [Utility prettyPrintMyString:@"This is a test"];

    Which brings up a very important point (in my mind) and one that
    hasn't seemed to surface. Chad (the original poster) asked the
    following question:

    "What would be the best way to [build utility functions] in a Cocoa
    application?"

    Chad, if you're using Objective C (not the Java-Cocoa stuff) then the
    shortest and simplest answer I can think of is by using C functions.
    Add .c files to your project and export the functions in an .h file.
    If they truly are utility functions that are "first order" (to use
    functional programming parlance), then let them be. Objective C plays
    well with C with no major hiccups.

    This:

    "Because Objective-C rests on a foundation of ANSI C, you can freely
    intermix straight C code with Objective-C code."

    ...is straight from the horse's mouth: <http://developer.apple.com/
    documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/
    chapter_2_section_2.html
    >

    -daniel
    --
    http://www.hedrick.org/daniel?sig
  • I've found "including" the .h file, providing the header (.h file) &
    function code (.c file), and calling the function as a 'C' function
    (not a method on an object) from within the Objective 'C' code works
    fine.  Depends how purist you are.  There are facilities for any
    necessary conversions.  Is there a "proper" way?  I hope others may
    enlighten both of us if there is!  I'd always understood an advantage
    of "Cocoa" is that you can mix Objective-C, C++ and C.

    david
    --------
    David Hill
    --------
    Simplicity, patience, compassion. These three are your greatest
    treasures  (Tao Te Ching #67)
    ---------

    On Oct 24, 2006, at 7:52 PM, Chad Armstrong wrote:

    > This is probably a fairly basic question (I hope), but I want to
    > make sure that I'm going about this the proper way.
    >
    > With some older C/C++ applications I wrote years ago, if there was
    > a set of commonly used functions, I would throw them into a .h file
    > so I could easily reference those functions from pretty much
    > anywhere inside the application.
    >
    > What would be the best way to do a similar thing in a Cocoa
    > application?  Just writing a .h file and throw in the functions?
    > Or would I have to wrap a class around each function?  Write a
    > framework, perhaps?  Or is there some other way about doing things
    > that I'm just entirely missing?
    >
    > Ultimately, I'd like to be able to write some code similar to this
    > in my code which would refer to the function/method in the commonly
    > shared file.
    >
    > [self myMethod: someVariable];
    >
    >
    > Chad
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
  • El 25/10/2006, a las 4:52, Chad Armstrong escribió:

    > This is probably a fairly basic question (I hope), but I want to
    > make sure that I'm going about this the proper way.
    >
    > With some older C/C++ applications I wrote years ago, if there was
    > a set of commonly used functions, I would throw them into a .h file
    > so I could easily reference those functions from pretty much
    > anywhere inside the application.
    >
    > What would be the best way to do a similar thing in a Cocoa
    > application?  Just writing a .h file and throw in the functions?
    > Or would I have to wrap a class around each function?  Write a
    > framework, perhaps?  Or is there some other way about doing things
    > that I'm just entirely missing?
    >
    > Ultimately, I'd like to be able to write some code similar to this
    > in my code which would refer to the function/method in the commonly
    > shared file.
    >
    > [self myMethod: someVariable];

    http://wilshipley.com/blog/2005/11/frameworks-are-teh-suck-err.html
  • On Oct 25, 2006, at 2:53 AM, leenoori wrote:

    > El 25/10/2006, a las 4:52, Chad Armstrong escribió:
    >
    >> This is probably a fairly basic question (I hope), but I want to
    >> make sure that I'm going about this the proper way.
    >>
    >> With some older C/C++ applications I wrote years ago, if there was
    >> a set of commonly used functions, I would throw them into a .h
    >> file so I could easily reference those functions from pretty much
    >> anywhere inside the application.
    >>
    >> What would be the best way to do a similar thing in a Cocoa
    >> application?  Just writing a .h file and throw in the functions?
    >> Or would I have to wrap a class around each function?  Write a
    >> framework, perhaps?  Or is there some other way about doing things
    >> that I'm just entirely missing?
    >>
    >> Ultimately, I'd like to be able to write some code similar to this
    >> in my code which would refer to the function/method in the
    >> commonly shared file.
    >>
    >> [self myMethod: someVariable];
    >
    > http://wilshipley.com/blog/2005/11/frameworks-are-teh-suck-err.html

    That's a very interesting post but I think that's more advanced than
    what the OP was looking for.  That said, I agree with Wil about the
    complicated mess that shared framework can pose (I've written many
    myself under NextStep/OpenStep and OSX) and like his novel approach
    to the problem using svn.
previous month october 2006 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 31          
Go to today