About [NSArray alloc] initWithObjects: problem

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

  1. andyiapan macrumors newbie

    Feb 28, 2010
    if i got an id *object
    object[size] may be 20000,
    but i want to

    NSArray *listContent = [[NSArray alloc] initWithObjects:
    [Product productWithType:eek:bjects[i+1] name:eek:bjects]

    how can i alloc all the objects in a listContent, i can't find out this use a for loop, thx
  2. andyiapan thread starter macrumors newbie

    Feb 28, 2010
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"txt"];
    	id *objects;
    	int i=0;
    		NSString *myText = [NSString stringWithContentsOfFile:filePath];  
    			int stringLength = [myText length];
    			NSRange range = NSMakeRange(0, stringLength);
    			NSString *newStr = [myText stringByReplacingOccurrencesOfString:@"\n" withString:@"	" options:NSCaseInsensitiveSearch range:range];
    			NSArray *listItems = [newStr componentsSeparatedByString:@"	"];
    			NSUInteger count = [listItems count];
    			objects = malloc(sizeof(id *) * count);
    			[listItems getObjects:objects];
    		NSArray *listContent = [[NSArray alloc] initWithObjects:
    							[Product productWithType:objects[i+1]  name:objects[i]],nil];
    if i want to initWithObjects:  
    productWithType:objects[1]  name:objects[0]],
    productWithType:objects[3]  name:objects[2]],
    i need to input one by one,
    but i got objects[20000], can i use i for loop or other methods to initWithObjects? ThX
  3. skunkworker macrumors regular

    Sep 9, 2007
    First of all please use the CODE bbtag for the code.
    The "id" tag is the pointer to the object. You don't need to create a id*, unless you want a pointer to a pointer.
    Why exactly are you using this line?
     objects = malloc(sizeof(id *) * count); 
    You could just use a NSMutableArray and add Objects to it.
    like so.
    NSMutableArray *array = [NSMutableArray];
    for (int i=0; i< 15; i++)
       [array addObject:[MyClass constructorWithString:@"String"]];
    And to access objects in an array use the objectAtIndex method.
    Like so.
    Class *myObject = [array objectAtIndex:8];
    id myObject = [array objectAtIndex:8];
    They both do the same thing
  4. andyiapan thread starter macrumors newbie

    Feb 28, 2010
    i want to 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,

    as i want to build a apps like that but i feel frustrate.
    and i just use TableSearch example as reference but comes out many problem:(
  5. andyiapan thread starter macrumors newbie

    Feb 28, 2010
    any searching algorithm or access text file(database) example
    that i can as reference in the above case? THX

Share This Page