Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary using call method



On Sunday, Mar 6, 2005, at 05:41 US/Pacific, Tetsuro KURITA wrote:

I think that "setObject:forKey:" method does not works.

Tetsuro,

I see the problem. It's not really with "setObject:forKey:" -- otherwise thousands of Cocoa developers would complain. It's with NSMutableDictionary creation in AppleScript Studio.

When you create an NSDictionary with some supplied keys and values, the class of the created object is 'reco'. But, when you create an NSMutableDictionary with capacity, the class of the created object is 'list'. But it seems not to be a regular list object. Maybe there's a special way to add dictionary values to it but I think it is made the way it is because of traditional AppleScript.

However, I have a solution that might be good for you. It is attached. It shows how to use some calls of NSDictionary. If you need others, they should be quite easy to add.

Attachment: nsdict.html
Description: application/applefile

Title: Using NSDictionary in AppleScript Studio
-- AppleScript Studio AppleScript for using NSDictionary
--
Created 2005-03-06 by Philip Aker

-- Put all the NSDictionary stuff in a script so the code doesn't get messed up with the app logic
script mute
    
property reco : missing value
    
on add ( theKey , theValue )
        
if ( reco is equal to missing value ) then
            
set reco to call method " dictionaryWithObjects:forKeys: " of class " NSDictionary " with parameters {{ theValue }, { theKey }}
        
else
            
set dict to call method " dictionaryWithObjects:forKeys: " of class " NSDictionary " with parameters {{ theValue }, { theKey }}
            
set reco to reco & dict
        
end if
    
end add
    
on add_many ( theKeys , theValues )
        
if ( reco is equal to missing value ) then
            
set reco to call method " dictionaryWithObjects:forKeys: " of class " NSDictionary " with parameters { theValues , theKeys }
        
else
            
set dict to call method " dictionaryWithObjects:forKeys: " of class " NSDictionary " with parameters { theValues , theKeys }
            
set reco to reco & dict
        
end if
    
end add_many
    
on value ( theKey )
        
if ( reco is equal to missing value ) then
            
return missing value
        
else
            
call method " objectForKey: " of reco with parameter theKey
        
end if
    
end value
    
on values ()
        
return call method " allValues " of reco
    
end values
    
on write_file ( thePath )
        
call method " writeToFile:atomically: " of reco with parameters { thePath , true }
    
end write_file
end script

-- Now illustrate usage of our NSDictionary script functions
on awake from nib theObject
    
if ( name of theObject is " main_menu ") then
        
try
            
mute 's add (" first ", " value 1 ")
            
mute 's add (" second ", " value 2 ")
            
mute 's add (" third ", list folder ( path to preferences from local domain ))
            
set val to mute 's value (" second ")
            
log val
            
set res to mute 's values ()
            
log res
            
set dpath to ( POSIX path of ( path to desktop )) & " reco.xml "
            
mute 's write_file ( dpath )
        
on error errs number errn
            
log errs & " : " & errn as string
        
end try
    
end if
end awake from nib

Copyright © 2005, Philip Aker



Philip


I used "dictionaryWithObject:forKey:" method instead of "dictionaryWithCapacity:" as follows.

set theDict to call method "dictionaryWithObject:forKey:" of class "NSMutableDictionary" with parameters {"aaa", "hello"}


call method "setObject:forKey:" of theDict with parameters {"hey", "hello"}
set theDescription to call method "description" of theDict
log theDescription -- the result of this log statement is "{hello = aaa; }"


The instance of "NSMutableDictionary" is created. But I can not change contents of the instance.



Philip Aker http://www.aker.ca

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/email@hidden

This email sent to email@hidden

References: 
 >Re: NSMutableDictionary using call method (From: Tetsuro KURITA <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.