PDA

View Full Version : Cocoa addObjectsFromArray problem




Zala
May 25, 2009, 10:19 PM
- (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
May 25, 2009, 10:40 PM
- (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/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/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:
- (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
May 25, 2009, 10:42 PM
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
May 26, 2009, 02:12 AM
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:
- (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....?

//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
May 26, 2009, 08:23 AM
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
May 28, 2009, 03:26 AM
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