create array of numbers

Discussion in 'iPhone/iPad Programming' started by henkhenk, Nov 2, 2009.

  1. macrumors newbie

    Joined:
    Nov 2, 2009
    #1
    hi all:

    I want to keep track of some numbers. for this purpose I will need to use an array of numbers:

    should I create it and use it the following way or there is an easy way?

    NSMutablearray *array1;

    [array1 addObject:[NSString stringWithFormat:mad:"%d", count]];
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If they are really numbers why not use NSNumber instead of NSString?

    Or, if you know the size in advance, why not just use a C array?
     
  3. thread starter macrumors newbie

    Joined:
    Nov 2, 2009
    #3
    hi robbieduncan tanks for the replay.

    I dont know the size in advance. and if I will add the number to the array like this way:
    [array1 addObject:[NSString stringWithFormat:mad:"%d", count]];

    how I can read it back and assign it to an NSIntiger or int? (like if I want to select one value from the array by an objectIndex.)

    tanks
    henk
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    As per my above suggestion: don't store it like that. If it's a number don't convert it to a string. You are just asking for pain. Store it as a NSNumber.
     
  5. thread starter macrumors newbie

    Joined:
    Nov 2, 2009
    #5
    I had some problem with NSNumber.

    ok I will try it.

    tanks
     
  6. thread starter macrumors newbie

    Joined:
    Nov 2, 2009
    #6
    problem solved:

    to ad an int to NSMutableArray:
    int count;
    NSMutableArray *array1;
    NSNumber *mynum =[NSNumber numberWithInteger:count];
    [array1 addObject:mynum];


    to covert NSNumber in NSMutableArray to int:
    int x = [[array1 objectAtIndex:indeX] intValue];
     

Share This Page