(Newbie) Retrieving from NSMutableArray
-
Hi,
am trying to retrieve data from my NSMutableArray. The array contains
integers, and I am trying to write a method that will retrieve an
integer based on the index provided to the method... here is what I
have...
-(int)getTimeAtIndex:(int)index;
{
return [originalArray objectAtIndex:index];
}
I was relatively confident that this would work, but instead I get an
error "return makes integer from pointer without a cast" at compile
time. It is just a warning however, and the app compiles and launches.
Confused, I try...
-(int)getTimeAtIndex:(int)index;
{
return [originalArray objectAtIndex:3];
}
Same error.
In desperation, I try...
-(int)getTimeAtIndex:(int)index;
{
return [originalArray objectAtIndex:[NSNumber numberWithInt:index]];
}
Another pointer without a cast error this time.
I swear I have done this before, but can't seem to work it out, any
help would be very much appreciated.
Apologies if I'm being really dumb.
=Peter -
On Saturday, November 1, 2003, at 06:08 pm, Peter Laurens wrote:
> Hi,
>
> am trying to retrieve data from my NSMutableArray. The array contains
> integers, and I am trying to write a method that will retrieve an
> integer based on the index provided to the method... here is what I
> have...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:index];
> }
[snip]
> I swear I have done this before, but can't seem to work it out, any
> help would be very much appreciated.
>
> Apologies if I'm being really dumb.
Apology accepted ;-)
Try
return [[originalArray objectAtIndex:index] intValue];
Kind regards,
Alastair. -
Le 1 nov. 03, à 19:08, Peter Laurens a écrit :
> am trying to retrieve data from my NSMutableArray. The array contains
> integers, and I am trying to write a method that will retrieve an
> integer based on the index provided to the method... here is what I
> have...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:index];
> }
>
You array can only store objects : NSNumber for example.
So when you do [originalArray objectAtIndex:index] you get the NSNumber
representing the value.
If you want an "int" then you have to use [[originalArray
objectAtIndex:index] intValue]
--
Thomas Deniau
"Unix is user friendly. It's just selective when choosing friends." -
On Saturday, November 1, 2003, at 06:08 pm, Peter Laurens wrote:
> Hi,
>
> am trying to retrieve data from my NSMutableArray. The array contains
> integers, and I am trying to write a method that will retrieve an
> integer based on the index provided to the method... here is what I
> have...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:index];
> }
>
> I was relatively confident that this would work, but instead I get an
> error "return makes integer from pointer without a cast" at compile
> time. It is just a warning however, and the app compiles and launches.
>
> Confused, I try...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:3];
> }
>
> Same error.
> In desperation, I try...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:[NSNumber
> numberWithInt:index]];
> }
>
> Another pointer without a cast error this time.
>
> I swear I have done this before, but can't seem to work it out, any
> help would be very much appreciated.
nsarrays can not and do not hold int values (or other basic data types
like BOOLs or floats) only objects (or more precisely, only pointers to
objects). you'll either have to put your int values into some kind of
object (like nsvalue i think? - involves encoding it if memory servers
- can be a little bit tricky/convoluted - probably not the best thing
to do) or not use an nsarray at all and use a c array to hold your ints
instead (much more memory efficient, and probably speed efficient too).
yes i've just had a look at nsvalue's documentation:
An NSValue object is a simple container for a single C or Objective-C
data item. It can hold any of the scalar types such as int, float, and
char, as well as pointers, structures, and object ids. The purpose of
this class is to allow items of such data types to be added to
collection objects such as NSArrays and NSSets, which require their
elements to be objects. NSValue objects are always immutable.
i think the main problem with this is it's inefficient - you end up
using a lot more memory this way -
On Saturday, November 1, 2003, at 06:58 pm, Ben Dougall wrote:
> On Saturday, November 1, 2003, at 06:08 pm, Peter Laurens wrote:
>
> nsarrays can not and do not hold int values (or other basic data types
> like BOOLs or floats) only objects (or more precisely, only pointers
> to objects). you'll either have to put your int values into some kind
> of object (like nsvalue i think? - involves encoding it if memory
> servers - can be a little bit tricky/convoluted - probably not the
> best thing to do)
NSNumber would be better.
> or not use an nsarray at all and use a c array to hold your ints
> instead (much more memory efficient, and probably speed efficient > too).
NSArray is fast enough for most simple uses. You wouldn't use it in
the inner loop of a high-performance application (rendering engines,
games, scientific apps, etcetera), but it's fine for plenty of other
situations. It also has the advantage that you can store heterogeneous
data, which is a bit of a nuisance in C because you end-up with a
struct containing an enum and a union... Plus it's an Objective C
object, so it's reference counted and you can take advantage of all of
the built-in functionality in the framework.
Kind regards,
Alastair. -
On Nov 1, 2003, at 4:55 PM, Alastair J.Houghton wrote:
> On Saturday, November 1, 2003, at 06:58 pm, Ben Dougall wrote:
>
>> On Saturday, November 1, 2003, at 06:08 pm, Peter Laurens wrote:
>>
>> nsarrays can not and do not hold int values (or other basic data
>> types like BOOLs or floats) only objects (or more precisely, only
>> pointers to objects). you'll either have to put your int values into
>> some kind of object (like nsvalue i think? - involves encoding it if
>> memory servers - can be a little bit tricky/convoluted - probably not
>> the best thing to do)
>
> NSNumber would be better.
>
>> or not use an nsarray at all and use a c array to hold your ints
>> instead (much more memory efficient, and probably speed efficient >
>> too).
>
> NSArray is fast enough for most simple uses. You wouldn't use it in
> the inner loop of a high-performance application (rendering engines,
> games, scientific apps, etcetera), but it's fine for plenty of other
> situations. It also has the advantage that you can store
> heterogeneous data, which is a bit of a nuisance in C because you
> end-up with a struct containing an enum and a union... Plus it's an
> Objective C object, so it's reference counted and you can take
> advantage of all of the built-in functionality in the framework.
If you use a C array, you should use NSMutableData as the storage for
it instead of allocating memory yourself.
-bob -
On Saturday, November 1, 2003, at 03:28 PM, Bob Ippolito wrote:
> If you use a C array, you should use NSMutableData as the storage for
> it instead of allocating memory yourself.
Why?
-- Bruce
------------------------------------------------------------------------
Bruce Toback (602) 996-8601| My candle burns at both ends;
OPT, Inc. (800) 858-4507| It will not last the night;
11801 N. Tatum Blvd. Ste. 142 | But ah, my foes, and oh, my friends -
Phoenix AZ 85028 | It gives a lovely light.
<btoback...> | -- Edna St. Vincent Millay -
On Nov 1, 2003, at 6:02 PM, Bruce Toback wrote:
> On Saturday, November 1, 2003, at 03:28 PM, Bob Ippolito wrote:
>
>> If you use a C array, you should use NSMutableData as the storage for
>> it instead of allocating memory yourself.
>
> Why?
Because it's reference counted, serializable, you can put it in a
NSArray/NSDictionary/etc, pass it over DO, etc. It's also easier than
malloc/realloc/free.
-bob -
On dim, 2003-11-02 at 00:24, Bob Ippolito wrote:
>>> If you use a C array, you should use NSMutableData as the storage for
>>> it instead of allocating memory yourself.
>>
>> Why?
>
> Because it's reference counted, serializable, you can put it in a
> NSArray/NSDictionary/etc, pass it over DO, etc. It's also easier than
> malloc/realloc/free.
It also has a bigger overhead :-)
Hub
--
"<erno> hm. I've lost a machine.. literally _lost_. it responds to ping,
it works completely, I just can't figure out where in my apartment it
is." -- http://www.bash.org/?5273 -
On Nov 3, 2003, at 5:48 AM, Hubert Figuiere wrote:
> On dim, 2003-11-02 at 00:24, Bob Ippolito wrote:
>
>>>> If you use a C array, you should use NSMutableData as the storage
>>>> for
>>>> it instead of allocating memory yourself.
>>>
>>> Why?
>>
>> Because it's reference counted, serializable, you can put it in a
>> NSArray/NSDictionary/etc, pass it over DO, etc. It's also easier than
>> malloc/realloc/free.
>
> It also has a bigger overhead :-)
If you're allocating and deallocating a big array of memory hundreds of
thousand times a second, you have much bigger problems.
-bob -
Peter Laurens wrote:
> Hi,
>
> am trying to retrieve data from my NSMutableArray. The array contains
> integers, and I am trying to write a method that will retrieve an
> integer based on the index provided to the method... here is what I
> have...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:index];
> }
>
> I was relatively confident that this would work, but instead I get an
> error "return makes integer from pointer without a cast" at compile
> time. It is just a warning however, and the app compiles and launches.
>
> Confused, I try...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:3];
> }
>
> Same error.
> In desperation, I try...
>
> -(int)getTimeAtIndex:(int)index;
> {
> return [originalArray objectAtIndex:[NSNumber numberWithInt:index]];
> }
>
> Another pointer without a cast error this time.
>
> I swear I have done this before, but can't seem to work it out, any
> help would be very much appreciated.
>
> Apologies if I'm being really dumb.
>
> =Peter
Simple test..
NSArray *foo = [NSArray arrayWithObject:[NSNumber numberWithInt:5]];
int bar = [[foo objectAtIndex:0] intValue];
NSLog(@"bar = %i\n",bar);
SO... assuming that the array has NSNumbers..
-(int)getTimeAtIndex:(int)index;
{
return [[originalArray objectAtIndex:index] intValue];
}
chuck



