Breaking objects on the S & R… picking up from where I was last time.
After correcting some proofreading errors in the printed version I’m now stuck on a script object that has one nonworking handler.
Same code implemented as ‘standard’ handlers works.
This is the handler version:
property friendsAges : {{theKey:"Jan", theValue:27}, {theKey:"Sam", theValue:29}, {theKey:"Bob", theValue:35}}
on findRecordForKey(theKey)
repeat with refRecord in friendsAges
if theKey of refRecord = theKey then return refRecord
end repeat
return missing value
end findRecordForKey
on deleteItem(theKey)
set refRecord to findRecordForKey(theKey)
if refRecord is missing value then
error "The key wasn't found." number -1728 from theKey
end if
set contents of refRecord to missing value
set friendsAges to every record of friendsAges
return
end deleteItem
findRecordForKey("Bob") --> a pointer
deleteItem("Bob") -- no problem
friendsAges --> no Bob
And here’s the script object - same code, but deletion fails:
on makeAssociativeList()
script AssociativeList
property class : "associative list"
property listItems : {}
on findRecordForKey(theKey)
repeat with refRecord in my listItems
if theKey of refRecord = theKey then return refRecord
end repeat
return missing value
end findRecordForKey
on setItem(theKey, theValue)
set refRecord to findRecordForKey(theKey)
if refRecord = missing value then
set end of my listItems to {theKey:theKey, theValue:theValue}
else
set theValue of refRecord to theValue
end if
return
end setItem
on deleteItem(theKey)
set refRecord to findRecordForKey(theKey)
if refRecord is missing value then
error "The key wasn't found." number -1728 from theKey
end if
set contents of refRecord to missing value --> ERROR
set my listItems to every record of my listItems
return
end deleteItem
end script
end makeAssociativeList
set friendsAges to makeAssociativeList() -- create an object
tell friendsAges -- set some values
setItem("Jan", 27)
setItem("Sam", 29)
setItem("Bob", 35)
end tell
listItems of friendsAges --> same as before
deleteItem("Bob") of friendsAges
--> "Can't set item 3 of listItems of «script AssociativeList» to missing value." number -10006
The code is exactly as in the book, I only changed variable names to camel case.
I’ve left out handlers that the examples don’t use.
What’s wrong? Did I miss another proofreading error?