Duplicating an array controller's object has unexpected results...

Hello,

I was trying to implement a “duplicate” button to (obviously) duplicate selected items in a table view and though I believed it to be a simple task to do, I do get a result, but not what I expected.

The item(s) actually get duplicated (with a " copy" added at the end of the title, but the original entry also receives this new addition. Also, if i click on either the original or the copy, both gets selected (although this might be related to the fact that the names are the same).

Here is the code i’m using:

set selectedObjectsToDuplicate to theArrayController's selectedObjects()
set selectedObjectsToDuplicateMutable to current application's NSMutableArray's arrayWithCapacity_(255)
selectedObjectsToDuplicateMutable's addObjectsFromArray_(selectedObjectsToDuplicate)
repeat with currentItem in selectedObjectsToDuplicateMutable
	set entryTitle to currentItem's valueForKey_("publicationName")
	set newEntryTitle to current application's NSString's stringWithString_(((((entryTitle) as string) & " copy") as string))
	currentItem's setValue_forKey_(newEntryTitle, "publicationName")
end repeat
theArrayController's addObjects_(selectedObjectsToDuplicateMutable)

Anyone has any idea what could be wrong? Although I create a different NSMutableArray to change the objects, there seems to be a link still between both of them. How is this possible? I saw nothing in the docs that would explain this.

Thanks

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Even changing this:

set selectedObjectsToDuplicateMutable to current application's NSMutableArray's arrayWithCapacity_(255)

to:

set selectedObjectsToDuplicateMutable to current application's NSMutableArray's alloc()'s initWithCapacity_(255)

didn’t change the problem.

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Alright, found the solution. I “broke” the relationship between Cocoa objects by converting the selected objects NSArray to an applescript list. Works like a charm. Though I’d be curious to know how it could be solved in the Cocoa world… :slight_smile:

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

try the copy method from NSObject:

set selectedObjectsToDuplicate to theArrayController's selectedObjects()'s copy()

It’s the equivalent of the set vs copy thing in AS.

Ah, yes. Saw that one in the NSObject class reference I think. Thanks, I’ll try this.

Browser: Safari 6533.18.5
Operating System: Mac OS X (10.6)