Can't get array to work

G’day

The array ‘theGraphData’ is bound to a table with 2 columns. The bindings seem ok.

I’m trying to copy entries from another table, using ‘theArrayController2’.

This is a dilemma to me, if i use the script as below, the array does not get added to. But, if i use the commented out section, the array gets added to, but only a left bracket shows in the table columns the array is bound to.

The array is bound with an array controller ‘theArrayController’.


on selectNewClient_(sender)
		set theFlag to true
		tell theArrayController2
			set theItems to selectedObjects()
			set stringToCount to clientcount of theItems as string
			set stringToFind to clientname of theItems as string
		end tell
		try
			repeat with i in my theGraphData
				say 1
				set oneRow to i
				if stringToFind = theGraphClient of oneRow then
					set theFlag to false
					exit repeat
				end if
			end repeat
		on error
			set theFlag to true
		end try
		if theFlag then
			set my theGraphData to my theGraphData's arrayByAddingObject_({theGraphCount:stringToCount, theGraphClient:stringToFind}) --< Make Report list of Clients
			(* set AnItem to my theGraphData
			set end of AnItem to {theGraphCount:stringToCount, theGraphClient:stringToFind}
			set my theGraphData to AnItem*)
		end if
	end selectNewClient_

I’ve initialized it with …


	property theGraphData : {}
on applicationWillFinishLaunching_(aNotification)
	<otherstuff>
	tell current application's NSMutableArray to set theGraphData to arrayWithCapacity_(1)
	end applicationWillFinishLaunching_
	

Any thoughts as to what I’m doing wrong please?

Regards

Santa

You can save a lot of that repeating by using filteredArrayUsingPredicate:, something like:

set aPredicate to current application's NSPredicate's predicateWithFormat_("theGraphClient == %@", stringToFind)
set theList to theGraphData's filteredArrayUsingPredicate_(aPredicate)

To add an object to a mutable array, use addObject:

theGraphData's addObject_({theGraphCount:stringToCount, theGraphClient:stringToFind})

Thanks Shane.

Unfortuately I get the error

'The variable theList is not defined.

Regards

Santa



	on selectNewClient_(sender)
		set theFlag to true
		tell theArrayController2
			set theItems to selectedObjects()
			set stringToCount to clientcount of theItems
			set stringToFind to clientname of theItems
		end tell
		set theList to ""
		set aPredicate to current application's NSPredicate's predicateWithFormat_("theGraphClient == %@", stringToFind)
		set theList to theGraphData's filteredArrayUsingPredicate_(aPredicate)
		try
			if theList ≠ "" then
				say "found"
			else
				theGraphData's addObject_({theGraphCount:stringToCount, theGraphClient:stringToFind})
				set theGraphData to theGraphData
			end if
		on error errmsg
			display dialog errmsg
		end try
	end selectNewClient_

It probably returns missing value if there are no matches. Test for that.

Nope, same error sorry.

It’s almost as though the array has something wrong with it.

Regards

Santa


on selectNewClient_(sender)
		set theFlag to true
		tell theArrayController2
			set theItems to selectedObjects()
			set stringToCount to clientcount of theItems
			set stringToFind to clientname of theItems
		end tell
		set theList to ""
		set aPredicate to current application's NSPredicate's predicateWithFormat_("theGraphClient == %@", stringToFind)
		set theList to theGraphData's filteredArrayUsingPredicate_(aPredicate)
		try
			if theList ≠ missing value then
				say "found"
			else
				theGraphData's addObject_({theGraphCount:stringToCount, theGraphClient:stringToFind})
				set theGraphData to theGraphData
			end if
		on error errmsg
			display dialog errmsg
		end try
	end selectNewClient_

In that case the problem is somewhere other than the code you’re posting.

I was wrong about missing value being returned – you should get an empty array. Anyway, this works fine here:

set theGraphData to {{theGraphClient:"aaa", theName:3}, {theGraphClient:"bb", theName:3}}
set theGraphData to current application's NSMutableArray's arrayWithArray_(theGraphData)
set stringToFind to "aaa"
set aPredicate to current application's NSPredicate's predicateWithFormat_("theGraphClient == %@", stringToFind)
set theList to theGraphData's filteredArrayUsingPredicate_(aPredicate)
log theList

Start adding some logging and see where it’s going wrong. Logging theGraphData’s |class|() is probably a good place to start.

Thanks again Shane

I tracked it down, i had a piece of code I’d overlooked that set the array to {}.

However, I still get a situation where tha table gets filled with single left brackets ‘(’ instead of the correct information. I’ve checked, and the array is definitely being populated by the correct information, but it’s not displaying correctly.

I even replaced the table with a new one from the Library, and re-did the bindings. Any ideas please anyone?

Regards

Santa


on selectNewClient_(sender)
		set theFlag to true
		tell theArrayController2
			set theItems to selectedObjects()
			set stringToCount to clientcount of theItems
			set stringToFind to clientname of theItems
		end tell
		set aPredicate to current application's NSPredicate's predicateWithFormat_("theGraphClient == %@", stringToFind)
		set theList to theGraphData's filteredArrayUsingPredicate_(aPredicate)
		try
			if theList's |count|() > 0 then
				say "found"
			else
				my theGraphData's addObject_({theGraphCount:stringToCount, theGraphClient:stringToFind})
				set my theGraphData to my theGraphData --< without this, the table won't update with anything at all, why?
			end if
		on error errmsg
			display dialog errmsg
		end try
	end selectNewClient_

Are you sure stringToFind is a string and not an array of strings?

Good morning Shane

Yes, the variable stringtofind is OK. The rest of my program works alright using the array that’s built up, just the display doesn’t work.

I’m not skilled at bindings, so have to wonder about them?

Why would it constantly only show ‘(’ when none of the strings of the array start with ‘(’ ?

Regards

Santa

Why would it constantly only show '(' when none of the strings of the array start with '(' ?

Have a look at what an NSArray looks like in the log: it begins with a “(” on a line of its own. It’s possibly a bindings error, or what you think is a string is actually an array of strings.