Structure Problem of NSArray

Discussion in 'iOS Programming' started by andyiapan, Feb 28, 2010.

  1. andyiapan macrumors newbie

    Joined:
    Feb 28, 2010
    #1
    Code:
    id *objects;
    
    NSArray *listItems = [newStr componentsSeparatedByString:@"	"];
    NSUInteger count = [listItems count];
    objects = malloc(sizeof(id *) * count);
    [listItems getObjects:objects];
    
    NSArray *listContent = [[NSArray alloc] initWithObjects:
    [Product productWithType:objects[1]  name:objects[0]],
    [Product productWithType:objects[3]  name:objects[2]],
    [Product productWithType:objects[5]  name:objects[4]],
    [Product productWithType:objects[7]  name:objects[6]], nil];
    
    i want to create different name of objects[0], object[2],.....object[count]
    count == 10000
    i need to copy and paste it one by one or i can use some way to solve it,
    ThXXX:(

    Code:
    NSArray *listContent = [[NSArray alloc] initWithObjects:
    [Product productWithType:objects[1]  name:objects[0]],
    [Product productWithType:objects[3]  name:objects[2]],
    [Product productWithType:objects[5]  name:objects[4]],
    [Product productWithType:objects[7]  name:objects[6]],
    [Product productWithType:objects[9]  name:objects[8]],
    [Product productWithType:objects[11]  name:objects[10]],
    [Product productWithType:objects[13]  name:objects[12]],
    [Product productWithType:objects[15]  name:objects[14]], ...........nil];
    that mean i need to[Product productWithType:eek:bjects[] name:eek:bjects[]],
    repeatedly, i just learn Xcode and i don't know the structure about it,
    can someone help me:(?
     
  2. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #2
    First, there may be something structurally wrong with the architecture of your program if you need to take something that is already stored into a giant array - can't you just walk over te Product thingy using a loop whenever you need to access it as if it is an array?

    In any event, to answer your specific question, you could use a mutablearray and a loop, for example.

    Code:
    NSMutableArray *listContent=[[NSMutableArray alloc] initWithCapacity:1000];
    
    for (int x=0;x<1000;x++) {
        [listContent addObject:[Product productWithType: objects[x+1] name: objects[x]];
    }
     
  3. andyiapan thread starter macrumors newbie

    Joined:
    Feb 28, 2010
    #3
    thank's your help,
    can't you just walk over te Product thingy using a loop whenever you need to access it as if it is an array?
    that mean if i use a mutablearray and a for loop in your example?
    why can't use NSArray but can use NSMutableArray in this case?
    Just a noob question:(

    the thing i want to do is load a text file that content is
    123456 A
    324123 B
    687678 C
    23 D
    111111 E

    as i see want to separated number and Text
    when i input number 1 , it will show A and E,
    12, it will show A, 123456 also A, but i still can't understand the structure or function in Xcode:(
     
  4. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #4
    NSArray's cannot be changed once they are created - hence they do not have an addObject member function.

    Regarding your function, there are lots of way to do what you want. I assume you read the file once, but do lots of queries? And you only want 12 to return A (not B, which has a "12" in it but doesn't start with 12?)

    Is there a length limitation on the number? (maximum number of digits). Depending on what you are trying to do, you might want a data structure like a tree. In other words, given:

    123456 A
    324123 B
    687678 C
    23 D
    111111 E

    your structure would be something like:

    1
    / \
    123456 111111
    =A =E

    etc.

    If you want something rough and dirty just store the numbers (as NSStrings) as keys in an NSDictionary and the names as the objects in the NSDictionary, and walk through the keys and check if each "hasPrefix" 1, 12, or whatever the user enters.

    That won't be ideal given a thousand keys, but it may suit your purposes.
     

Share This Page