[Mono-osx] [MonoMac] NSMutableDictionary FromObjectsAndKeys problem

kjpou kjpou at pt.lu
Tue Nov 16 01:13:45 EST 2010


Miguel

Refreshed from master and verified that it works.

Thanks

Kenneth

On 11/16/10 6:43 AM, Miguel de Icaza wrote:
> Hello,
>
>     I started looking at it and there are two ways of approaching the
>     problem that I found.
>
>
> Oops, good catch!
>
> I got a fix very similar to yours, they now both use [New] and I 
> removed the code that checked, as the underlying runtime should be 
> catching that anyways;
>
> Miguel
>
>
>     1) The easiest was the following:
>
>     Add the following to foundation.cs for interface
>     NSMutableDictionary specifying [New]
>
>            [Export ("dictionaryWithObjects:forKeys:")]
>            [Static,New]
>            NSMutableDictionary FromObjectsAndKeys (NSObject []
>     objects, NSObject [] Keys);
>
>     *** Note ***    If [New] is not specified it will not work
>     correctly because it uses the base NSDictionary method and the
>     Dictionary returned is not Mutable.
>
>     Also commenting out the following code in NSMutableDictionary.cs
>
>            public static NSMutableDictionary FromObjectsAndKeys
>     (NSObject [] objects, NSObject [] keys)
>            {
>                if (objects.Length != keys.Length)
>                    throw new ArgumentException ("objects and keys
>     arrays have different sizes");
>                return FromObjectsAndKeys (objects, keys);
>            }
>
>     2)  The second approach was keeping the ArgumentException check in
>     NSMutableDictionary.cs and defining a new internal method in
>     foundation.cs
>
>     NSMutableDictionary.cs
>
>            public static NSMutableDictionary FromObjectsAndKeys
>     (NSObject [] objects, NSObject [] keys)
>            {
>                if (objects.Length != keys.Length)
>                    throw new ArgumentException ("objects and keys
>     arrays have different sizes");
>                return FromObjectsAndKeysInternal (objects, keys);
>            }
>
>     foundation.cs
>
>            [Export ("dictionaryWithObjects:forKeys:")]
>            [Static,Internal]
>            NSMutableDictionary FromObjectsAndKeysInternal (NSObject []
>     objects, NSObject [] Keys);
>
>
>     I chose the second approach to keep the ArgumentException check in
>     with the least amount of changes.
>
>     Either one works.
>
>     I have attached the diff if you choose the second one.
>
>     Shouldn't NSDictionary include the same ArgumentException check as
>     NSMutableDictionary if that is the way you decide?  Just for
>     consistency if anything.
>
>     Kenneth
>
>
>
>
>     On 11/16/10 12:47 AM, Miguel de Icaza wrote:
>
>         Hello,
>
>             I am working with a mutable dictionary with bindings to an
>             ArrayController
>             and am having problems creating a NSMutableDictionary
>             using the
>             FromObjectsAndKeys.
>
>         Are you running the latest version from GitHub?
>
>         On Friday or so, Geoff fixed an important bug with arrays in
>         the Monomac
>         runtime.
>
>             It works for a NSDictionary but not when creating a
>             NSMutableDictionary.
>
>             The example code below shows what I am talking about.
>
>                    var objects = new NSObject[3] {new NSString("AAAAA"),
>                                                            new
>             NSString("BBBBB"),
>                                                            new
>             NSString("CCCCCC")};
>
>                    var keys = new NSObject[3] {new NSString("firstkey"),
>                                                            new
>             NSString("secondkey"),
>                                                            new
>             NSString("thirdkey")};
>
>                    var dict2 =
>             NSDictionary.FromObjectsAndKeys(objects,keys);
>
>                    //var dict =
>             NSMutableDictionary.FromObjectsAndKeys(objects, keys);
>                    var dict = NSMutableDictionary.FromDictionary(dict2);
>
>                    myContentArray.AddObject(dict);
>
>             The implementation between NSDictionary.cs and
>             NSMutableDictionary.cs are
>             different for that method and not sure why.
>
>             Any help would be appreciated
>
>             Kenneth
>
>             ----------------------------------
>             If you uncomment the //var dict =
>             NSMutableDictionary.FromObjectsAndKeys(objects, keys); it
>             gives the
>             following error:
>
>             Stacktrace:
>
>               at (wrapper managed-to-native)
>             MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr_int
>             (intptr,intptr,intptr,intptr,int)<0x00004>
>               at (wrapper managed-to-native)
>             MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr_int
>             (intptr,intptr,intptr,intptr,int)<0x00004>
>               at MonoMac.Foundation.NSMutableDictionary.FromObjectsAndKeys
>             (MonoMac.Foundation.NSObject[],MonoMac.Foundation.NSObject[],int)<0x00067>
>               at MonoMac.Foundation.NSMutableDictionary.FromObjectsAndKeys
>             (MonoMac.Foundation.NSObject[],MonoMac.Foundation.NSObject[])<0x00028>
>               at NSTableViewBinding.TestWindowController.AwakeFromNib
>             ()<0x001f8>
>               at (wrapper native-to-managed)
>             object.92f48794-05ff-4e21-bcf0-61c0dd4bf42b
>             (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector)<0x000cc>
>               at (wrapper managed-to-native)
>             MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper
>             (intptr,intptr)
>             <0x00004>
>               at (wrapper managed-to-native)
>             MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper
>             (intptr,intptr)
>             <0x00004>
>               at MonoMac.AppKit.NSWindowController.get_Window ()<0x00079>
>               at NSTableViewBinding.TestWindowController.get_Window
>             ()<0x00012>
>               at NSTableViewBinding.AppDelegate.FinishedLaunching
>             (MonoMac.Foundation.NSObject)<0x00038>
>               at (wrapper native-to-managed)
>             object.00860f4e-27a8-4ca0-aa64-ae2828705416
>             (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject)
>             <0x00118>
>               at (wrapper managed-to-native)
>             MonoMac.AppKit.NSApplication.NSApplicationMain
>             (int,string[])<0x00004>
>               at (wrapper managed-to-native)
>             MonoMac.AppKit.NSApplication.NSApplicationMain
>             (int,string[])<0x00004>
>               at MonoMac.AppKit.NSApplication.Main (string[])<0x00015>
>               at NSTableViewBinding.MainClass.Main (string[])<0x00015>
>               at (wrapper
>             runtime-invoke)<Module>.runtime_invoke_void_object
>             (object,intptr,intptr,intptr)<0x00043>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20101116/795642e0/attachment.html 


More information about the Mono-osx mailing list