a simple task for Simula based languages

  • //consider this code for a repetitive task  3 time here but it could
    be 30 times

    dostufffor("This");
    dostufffor("and");
    dostufffor("other");

    dostufffor("That");
    dostufffor("and");
    dostufffor("another");

    dostufffor("Those");
    dostufffor("and");
    dostufffor("others");

    //for a number of obvious reasons one would want to rewrite the code
    as below

    mystuff ("This","and","other");
    mystuff ("That","and","another");
    mystuff ("Those","and","others");

    void mystuff (char First, char Second, char Third)
    {
      dostufffor(First);
      dostufffor(Second);
      dostufffor(Third);
    }

    //the question is , what is the best way to do this objective c ?
  • If you change "char" to "char*" in your code snippet below, this
    would work as written.
    This seems like you just need to learn how C works. This isn't an
    Objective-C issue at all.

    On Nov 17, 2007, at 1:15 AM, Vlad Alexa wrote:

    > //consider this code for a repetitive task  3 time here but it
    > could be 30 times
    >
    > dostufffor("This");
    > dostufffor("and");
    > dostufffor("other");
    >
    > dostufffor("That");
    > dostufffor("and");
    > dostufffor("another");
    >
    > dostufffor("Those");
    > dostufffor("and");
    > dostufffor("others");
    >
    >
    > //for a number of obvious reasons one would want to rewrite the
    > code as below
    >
    > mystuff ("This","and","other");
    > mystuff ("That","and","another");
    > mystuff ("Those","and","others");
    >
    > void mystuff (char First, char Second, char Third)
    > {
    > dostufffor(First);
    > dostufffor(Second);
    > dostufffor(Third);
    > }
    >
    >
    > //the question is , what is the best way to do this objective c ?
  • Sorry for not having the most correct code for illustration purposes.
      It is a objective-c issue i am afraid however because if i have to
    return something like a NSMutableString i have to write it all in obj-
    c and with my limited experience of the language i have yet to find a
    way that looks good , so that is why i am asking.
    And just for further reference the 3 consecutive dostuff calls are
    supposed to be different calls .

    On Nov 17, 2007, at 5:36 PM, John Stiles wrote:

    > If you change "char" to "char*" in your code snippet below, this
    > would work as written.
    > This seems like you just need to learn how C works. This isn't an
    > Objective-C issue at all.
    >
    >
    > On Nov 17, 2007, at 1:15 AM, Vlad Alexa wrote:
    >
    >> //consider this code for a repetitive task  3 time here but it
    >> could be 30 times
    >>
    >> dostufffor("This");
    >> dostufffor("and");
    >> dostufffor("other");
    >>
    >> dostufffor("That");
    >> dostufffor("and");
    >> dostufffor("another");
    >>
    >> dostufffor("Those");
    >> dostufffor("and");
    >> dostufffor("others");
    >>
    >>
    >> //for a number of obvious reasons one would want to rewrite the
    >> code as below
    >>
    >> mystuff ("This","and","other");
    >> mystuff ("That","and","another");
    >> mystuff ("Those","and","others");
    >>
    >> void mystuff (char First, char Second, char Third)
    >> {
    >> dostufffor(First);
    >> dostufffor(Second);
    >> dostufffor(Third);
    >> }
    >>
    >>
    >> //the question is , what is the best way to do this objective c ?
    >>
    >>
    >
  • On Nov 17, 2007, at 10:50 AM, Vlad Alexa wrote:

    > Sorry for not having the most correct code for illustration purposes.

    When requesting help (in any language) it's usually best to post what
    you've already written so far. This makes your intention clearer. If
    you need to keep details hidden for whatever reason you can change
    variable, class and method names in the snippet you post.

    regards,

    Ralph

    Raffael Cavallaro, Ph.D.
    <raffaelcavallaro...>
  • If you want to invoke the same method with a number of different
    arguments and collect the results using a convenient syntax, for
    example, a number of people have developed techniques for higher-order
    messaging in objective-c over the years.  Here's a paper

    http://www.metaobject.com/papers/Higher_Order_Messaging_OOPSLA_2005.pdf

    Cheers,
    dave

    On 17-Nov-07, at 8:50 AM, Vlad Alexa wrote:

    > Sorry for not having the most correct code for illustration purposes.
    > It is a objective-c issue i am afraid however because if i have to
    > return something like a NSMutableString i have to write it all in
    > obj-c and with my limited experience of the language i have yet to
    > find a way that looks good , so that is why i am asking.
    > And just for further reference the 3 consecutive dostuff calls are
    > supposed to be different calls .
    >
    > On Nov 17, 2007, at 5:36 PM, John Stiles wrote:
    >
    >> If you change "char" to "char*" in your code snippet below, this
    >> would work as written.
    >> This seems like you just need to learn how C works. This isn't an
    >> Objective-C issue at all.
    >>
    >>
    >> On Nov 17, 2007, at 1:15 AM, Vlad Alexa wrote:
    >>
    >>> //consider this code for a repetitive task  3 time here but it
    >>> could be 30 times
    >>>
    >>> dostufffor("This");
    >>> dostufffor("and");
    >>> dostufffor("other");
    >>>
    >>> dostufffor("That");
    >>> dostufffor("and");
    >>> dostufffor("another");
    >>>
    >>> dostufffor("Those");
    >>> dostufffor("and");
    >>> dostufffor("others");
    >>>
    >>>
    >>> //for a number of obvious reasons one would want to rewrite the
    >>> code as below
    >>>
    >>> mystuff ("This","and","other");
    >>> mystuff ("That","and","another");
    >>> mystuff ("Those","and","others");
    >>>
    >>> void mystuff (char First, char Second, char Third)
    >>> {
    >>> dostufffor(First);
    >>> dostufffor(Second);
    >>> dostufffor(Third);
    >>> }
    >>>
    >>>
    >>> //the question is , what is the best way to do this objective c ?
    >>>
    >>>
    >>

    >
  • Trying to be as explicit as i can here is the actual code :

    items=[[NSMutableDictionary alloc] init];

    NSToolbarItem *item=[[NSToolbarItem alloc]
    initWithItemIdentifier:@"Name"];
    [item setPaletteLabel:@"Name"];
    [item setLabel:@"Name"];
    [item setToolTip:@"Tip"];
    [item setImage: [NSImage imageNamed:@"icon"]];
    [item setTarget:self];
    [item setAction:@selector(toolbaritemclick:)];
    [items setObject:item forKey:@"Name"];

    NSToolbarItem *item=[[NSToolbarItem alloc]
    initWithItemIdentifier:@"Name1"];
    [item setPaletteLabel:@"Name1"];
    [item setLabel:@"Name1"];
    [item setToolTip:@"Tip1"];
    [item setImage: [NSImage imageNamed:@"icon1"]];
    [item setTarget:self];
    [item setAction:@selector(toolbaritemclick1:)];
    [items setObject:item forKey:@"Name1"];

    the obvious scope is to be able to call this code only once for each
    button and  besides it have only one line of code for each button
    that supplies the parameters name,tip,icon and action.

    On Nov 17, 2007, at 6:05 PM, Raffael Cavallaro wrote:

    >
    > On Nov 17, 2007, at 10:50 AM, Vlad Alexa wrote:
    >
    >> Sorry for not having the most correct code for illustration purposes.
    >
    > When requesting help (in any language) it's usually best to post
    > what you've already written so far. This makes your intention
    > clearer. If you need to keep details hidden for whatever reason you
    > can change variable, class and method names in the snippet you post.
    >
    > regards,
    >
    > Ralph
    >
    >
    > Raffael Cavallaro, Ph.D.
    > <raffaelcavallaro...>
  • On Nov 17, 2007, at 8:29 AM, Vlad Alexa wrote:
    > Trying to be as explicit as i can here is the actual code :
    >
    > items=[[NSMutableDictionary alloc] init];
    >
    > NSToolbarItem *item=[[NSToolbarItem alloc]
    > initWithItemIdentifier:@"Name"];
    > [item setPaletteLabel:@"Name"];
    > [item setLabel:@"Name"];
    > [item setToolTip:@"Tip"];
    > [item setImage: [NSImage imageNamed:@"icon"]];
    > [item setTarget:self];
    > [item setAction:@selector(toolbaritemclick:)];
    > [items setObject:item forKey:@"Name"];
    >
    > NSToolbarItem *item=[[NSToolbarItem alloc]
    > initWithItemIdentifier:@"Name1"];
    > [item setPaletteLabel:@"Name1"];
    > [item setLabel:@"Name1"];
    > [item setToolTip:@"Tip1"];
    > [item setImage: [NSImage imageNamed:@"icon1"]];
    > [item setTarget:self];
    > [item setAction:@selector(toolbaritemclick1:)];
    > [items setObject:item forKey:@"Name1"];
    >
    > the obvious scope is to be able to call this code only once for each
    > button and  besides it have only one line of code for each button
    > that supplies the parameters name,tip,icon and action.

    - (NSToolbarItem *) configureToolbarItem: (NSDictionary *)optionsDict
    {
    NSToolbarItem *item=[[NSToolbarItem alloc] initWithItemIdentifier:
    [optionsDict objectForKey: @"Name"]];
    [item setPaletteLabel: [optionsDict objectForKey: @"Name"]];
    [item setLabel: [optionsDict objectForKey: @"Name"]];
    [item setToolTip: [optionsDict objectForKey: @"Tip"]];
    [item setImage: [NSImage imageNamed: [optionsDict objectForKey:
    @"icon"]]];
    [item setTarget:self];
    [item setAction: NSSelectorFromString([optionsDict objectForKey:
    @"SEL"])];
    return item;
    }

    ....
    toolbarDict = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle
    mainBundle] pathForResource: @"ToolbarDescription" ofType: @"plist"]];
    items=[[NSMutableDictionary alloc] init];

    dict = [toolbarDict objectForKey: @"Item1 Configuration"];
    [items setObject: [self configureToolbarItem: dict] forKey: [dict
    objectForKey: @"Name"]];
    ...

    Where ToolbarDescription.plist would be a file containing a plist with
    each toolbars description in an array.  Something like:

    ...
    <array>
    <dict>
    <key>Name</key>
    <string>Bob The Button</string>
    <key>Image</key>
    <string>Bob The Image</string>
    ...
    </dict>
    <dict>
    ....
    </dict>
    ...
    </array>
    ...

    Consider the above pseudo code -- typed into Mail.

    That is how I solve this particular problem.  I like to push as much
    of the app configuration goop into property lists within the app
    wrapper;  user defaults, toolbar configurations, etc.

    b.bum
  • Kudos for that , your pseudocode was very good , below is how i
    implemented it , excluding the function which is basically identical
    to yours ...

    Now can anyone tell me if it is possible to further optimize/shrink
    this code here , maybe it should be done in another way ? it looks a
    bit overkill to me for its scope.

    And is it possible to generate dictionaries with automatic generated
    numerical indexes , i supply the indexes "one" and "two" but i do not
    actually use them.

    //init array with icon params and start adding
    params=[[NSMutableDictionary alloc] init];

    [params setObject:[NSDictionary dictionaryWithObjectsAndKeys:
                        @"Refresh", @"Name",
                        @"Refresh the list", @"Tip",
                        @"refresh", @"Icon",
                        @"toolbaritemclicked:", @"Act",
                        nil] forKey:@"one"];

    [params setObject:[NSDictionary dictionaryWithObjectsAndKeys:
                        @"Website", @"Name",
                        @"Visit us on the web", @"Tip",
                        @"refresh", @"Icon",
                        @"toolbaritemclicked1:", @"Act",
                     nil] forKey:@"two"];

    //create icons from params
    items=[[NSMutableDictionary alloc] init];
    id key;
    NSEnumerator *loop = [params keyEnumerator];
    while ((key = [loop nextObject])) {
    NSDictionary *dict = [params objectForKey:key];
        [items setObject:[self configureToolbarItem: dict] forKey:[dict
    objectForKey: @"Name"]];
    }

    On Nov 17, 2007, at 7:55 PM, Bill Bumgarner wrote:

    > On Nov 17, 2007, at 8:29 AM, Vlad Alexa wrote:
    >> Trying to be as explicit as i can here is the actual code :
    >>
    >> items=[[NSMutableDictionary alloc] init];
    >>
    >> NSToolbarItem *item=[[NSToolbarItem alloc]
    >> initWithItemIdentifier:@"Name"];
    >> [item setPaletteLabel:@"Name"];
    >> [item setLabel:@"Name"];
    >> [item setToolTip:@"Tip"];
    >> [item setImage: [NSImage imageNamed:@"icon"]];
    >> [item setTarget:self];
    >> [item setAction:@selector(toolbaritemclick:)];
    >> [items setObject:item forKey:@"Name"];
    >>
    >> NSToolbarItem *item=[[NSToolbarItem alloc]
    >> initWithItemIdentifier:@"Name1"];
    >> [item setPaletteLabel:@"Name1"];
    >> [item setLabel:@"Name1"];
    >> [item setToolTip:@"Tip1"];
    >> [item setImage: [NSImage imageNamed:@"icon1"]];
    >> [item setTarget:self];
    >> [item setAction:@selector(toolbaritemclick1:)];
    >> [items setObject:item forKey:@"Name1"];
    >>
    >> the obvious scope is to be able to call this code only once for
    >> each button and  besides it have only one line of code for each
    >> button that supplies the parameters name,tip,icon and action.
    >
    > - (NSToolbarItem *) configureToolbarItem: (NSDictionary *)optionsDict
    > {
    > NSToolbarItem *item=[[NSToolbarItem alloc] initWithItemIdentifier:
    > [optionsDict objectForKey: @"Name"]];
    > [item setPaletteLabel: [optionsDict objectForKey: @"Name"]];
    > [item setLabel: [optionsDict objectForKey: @"Name"]];
    > [item setToolTip: [optionsDict objectForKey: @"Tip"]];
    > [item setImage: [NSImage imageNamed: [optionsDict objectForKey:
    > @"icon"]]];
    > [item setTarget:self];
    > [item setAction: NSSelectorFromString([optionsDict objectForKey:
    > @"SEL"])];
    > return item;
    > }
    >
    > ....
    > toolbarDict = [NSDictionary dictionaryWithContentsOfFile:
    > [[NSBundle mainBundle] pathForResource: @"ToolbarDescription"
    > ofType: @"plist"]];
    > items=[[NSMutableDictionary alloc] init];
    >
    > dict = [toolbarDict objectForKey: @"Item1 Configuration"];
    > [items setObject: [self configureToolbarItem: dict] forKey: [dict
    > objectForKey: @"Name"]];
    > ...
    >
    > Where ToolbarDescription.plist would be a file containing a plist
    > with each toolbars description in an array.  Something like:
    >
    > ...
    > <array>
    > <dict>
    > <key>Name</key>
    > <string>Bob The Button</string>
    > <key>Image</key>
    > <string>Bob The Image</string>
    > ...
    > </dict>
    > <dict>
    > ....
    > </dict>
    > ...
    > </array>
    > ...
    >
    > Consider the above pseudo code -- typed into Mail.
    >
    > That is how I solve this particular problem.  I like to push as
    > much of the app configuration goop into property lists within the
    > app wrapper;  user defaults, toolbar configurations, etc.
    >
    > b.bum
  • --- Vlad Alexa <valexa...> wrote:

    > And is it possible to generate dictionaries with
    > automatic generated
    > numerical indexes , i supply the indexes "one" and
    > "two" but i do not
    > actually use them.

    I think you want an NSMutableArray or NSMutableSet
    rather than an NSMutableDictionary.

    Cheers,
    Chuck

          ____________________________________________________________________________________
    Be a better sports nut!  Let your teams follow you
    with Yahoo Mobile. Try it now.  http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
previous month november 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