Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Zala

macrumors newbie
Original poster
May 25, 2009
3
0
- (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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Chirone

macrumors 6502
Mar 2, 2009
279
0
NZ
i am gettin an warning here saying that NSArray may not respond to addObjectsFromArray.
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.
 

Zala

macrumors newbie
Original poster
May 25, 2009
3
0
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


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];
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Zala

macrumors newbie
Original poster
May 25, 2009
3
0
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


thanks lee it works like a charm now :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.