Cocoa addObjectsFromArray problem

Discussion in 'Mac Programming' started by Zala, May 25, 2009.

  1. macrumors newbie

    Joined:
    May 25, 2009
    #1
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {
    //Get the files from the drop
    NSArray *files = [[NSMutableArray alloc] init];
    files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
    /*
    //store filepath into string
    NSString *arrayElements = ([[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]);
    NSLog(@"value of arrayElements: %@", arrayElements);

    //add new elements to array
    storeArray = [[NSMutableArray alloc] init];
    //[storeArray addObject: arrayElements];*/

    storeArray = [[NSMutableArray alloc] init];

    int i = 0;
    int filesCount = [files count];

    for(i=0; i <filesCount; i++)
    {
    [storeArray addObjectsFromArray: [files objectAtIndex:i]];
    }

    i am gettin an warning here saying that NSArray may not respond to addObjectsFromArray.

    I am trying to get all the filepaths in the files array. But everytime when i drag a new file into the files array it gets overwritten. Therefore i am trying to consolidate all the files in the files array by copying them into the storeArray. Sorry for bad english.

    Appreciate your help thanks.
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Code:
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {
    	//Get the files from the drop
    	NSArray *files = [[NSMutableArray alloc] init];
    	files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
    	/*
    	//store filepath into string
    	NSString *arrayElements = ([[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]);
    	NSLog(@"value of arrayElements: %@", arrayElements);
    	
    	//add new elements to array
    	storeArray = [[NSMutableArray alloc] init];
    	//[storeArray addObject: arrayElements];*/
    	
    	storeArray = [[NSMutableArray alloc] init];
    	
    	int i = 0;
    	int filesCount = [files count];
    	
    	for(i=0; i <filesCount; i++)
    	{
    	[storeArray addObjectsFromArray: [files objectAtIndex:i]]; 
    	}
    }
    (Placed in code block for readability)

    That is the warning you should get, as NSArray does not respond to addObjectsFromArray:, NSMutableArray does.
    http://developer.apple.com/document...occ/instm/NSMutableArray/addObjectsFromArray:

    You also wouldn't do this in a loop, you would just pass files.

    Now a note about files. You allocate a new array, then init it, then immediately overwrite that pointer with a new pointer from propertyListForType:. I am totally unfamiliar with NSPasteboard, but from the docs it says this method returns id. I don't know if sometimes this is an NSArray, but if it is you would just send storeArray addObjectsFromArray:files. Also, you would need to make storeArray an instance variable, not a local variable in your method. This way it will continue to have things added to it, not just create it, add new things, then have access to it go away, leaking memory.

    So this might look like:
    Code:
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {
    	//Get the files from the drop
    	NSArray *files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]; //This should be autoreleased, so don't worry about files after this
    	[storeArray addObjectsFromArray:files]; //storeArray would need to be an NSMutableArray * declared as an instance variable, and alloc'd and init'd in your classes init/awakeFromNib, etc. method
    	
    	return YES; //I have no idea what this should return, so yes seems reasonable?
    }
    -Lee
     
  3. macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #3
    because such method doesn't exist for NSArray.
    from what i can tell NSArray just makes an immutable array and you can't do anything with it other than retrieve elements.
     
  4. thread starter macrumors newbie

    Joined:
    May 25, 2009
    #4

    thanks for the prompt reply guys... i tried ur method of declaring storeArray as NSMutable array over here i still get the same error hmmmm....?

    Code:
    //global variable : storeArray.
    const NSArray *storeArray;
    
    
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {
    	//Get the files from the drop
    	NSArray *files = [[NSMutableArray alloc] init];
    	files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
    	
    	//add new elements to array
    	storeArray = [[NSMutableArray alloc] init];
    	
    	[storeArray addObjectsFromArray: files];
    }
    
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    1) make store array an instance variable instead of global and initialize it in init or awakeFromNib.
    2) delclare it NSMutableArray
    3) const doesn't make sense here

    -Lee
     
  6. thread starter macrumors newbie

    Joined:
    May 25, 2009
    #6

    thanks lee it works like a charm now :D
     

Share This Page