swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dynamic arrays


From: Benedikt Stefansson
Subject: Re: dynamic arrays
Date: Wed, 20 May 1998 15:54:42 +0200

Hi James,

You can still use C arrays in Objective-C. It just depends on what you need the
array for, whether you want to use an Array instance to handle this.

Check any of the demo apps for how they create instances of a List object, since
both Array and List are Collection class objects and respond to many of the same
messages. List is in fact much more flexible, since it resizes dynamically as 
you
add/remove objects while Arrays expect to be static.

For basic questions such as yours always start by looking at  the HTML Swarmdocs
(absolute essential - download the tar file and unpack locally to speed access),
where you would for example have found all the info you need on the Array class.
The basics are in:
    swarmdocs/src/collections/Array.html
The create message for your particular case would be:

array=[Array create: [self getZone] setCount: 10*10];

Since Array is a subclass from Collection refer to
     swarmdocs/src/collections/Collection.html
for further info on how to add, remove and access members of the array. Since 
both
List and Array expect an id type, the best way to handle Arrays or Lists of
numbers is to create a small wrapper for floats, chars, ints, doubles etc. This 
is
an example of such a wrapper, that also remembers it's last state and can 
compare
itself to another object of it's type. You could obviously use something even
simpler.

#import <objectbase.h>

@implementation IntData: SwarmObject {
  int data;
  int lastData;
}

-setIntData: (int) d {
  lastData=d;
  data=d;
  return self;
}

-(int)compare: o {
  int val;

  if([o getIntData]==data){
    val=0;
  } else {
    if([o getIntData]<data)
      val=1;
    else
      val=-1;
  }

  return val;
}

-(int)getIntData{
  return data;
}
-(int)getLastIntData {
  return lastData;
}

@end

--
With best regards,
-Benedikt

James Marshall wrote:

> Hi everyone,
>   simple question... if someone can point out where I should be
> looking for absolute basics like these I'd be grateful!
>   I want to create an array of integers at runtime... in C++ I'd do
> the following:
>
> int *array, xsize, ysize;
> xsize=10;
> ysize=10;
> array=new int[xsize][ysize];
>
> How do I do this with Swarm and objective C? From my fiddlings it
> seems alloc is replaced by create... can you use the create message
> with the default types? Apologies for triviality.
>         James
> --
> James Marshall - Postgraduate Research Student (MPhil/PhD)
> Artificial Intelligence Group - Department of Computer and Information 
> Sciences
> De Montfort University - Milton Keynes Campus
> Web:- http://www.mk.dmu.ac.uk/~jmarshall/
>
>                   ==================================
>    Swarm-Support is for discussion of the technical details of the day
>    to day usage of Swarm.  For list administration needs (esp.
>    [un]subscribing), please send a message to <address@hidden>
>    with "help" in the body of the message.
>                   ==================================



-------------------------------------------------------------------
Department of Economics, UCLA, Los Angeles, CA 90095-1477, USA
Tel: (310) 825 4126     Fax: (310) 825 9528

4/3-7/1 1998:
Dipartimento di Economia, Universita di Trento, 38100 Trento, ITALY
Tel: +39 (461) 88 22 46 Fax: +39 (461) 88 22 22




                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]