Bug 390 - Cannot bind native libraries that return NSMutableArray
Summary: Cannot bind native libraries that return NSMutableArray
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2011-08-25 03:53 UTC by steve.keane
Modified: 2012-02-08 12:18 UTC (History)
7 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description steve.keane 2011-08-25 03:53:07 UTC
I am trying to bind the ShareKit API but several methods return a NSMutableArray

using latest stable versions of mono/monotouch

running bTouch gives an error as NSMutableArray is not in the framework.

Other people have run into this problem:  http://monotouch.2284126.n4.nabble.com/NSMutableArray-unavailable-BTouch-guru-needed-td3515333.html

Comment 1 Mikayla Hutchinson [MSFT] 2011-08-25 07:39:54 UTC
You can bind NSMutableArray values as NSArray, unless the API requires you to add/remove values to the mutable array.
Comment 2 Jeffrey Stedfast 2011-10-06 16:38:58 UTC
Is there a reason the API returns an NSMutableArray as opposed to an NSArray?

If the API is NSMutableArray getValues(), for example, is it expected that adding/removing or changing elements in that array are reflected in the object instance that you called getValues() on?
Comment 3 Jeffrey Stedfast 2011-10-10 13:08:06 UTC
Steve: *poke*
Comment 4 steve.keane 2011-10-11 09:01:21 UTC
Sorry for the delay in response.

Looking here: http://getsharekit.com/docs/ I cannot see any examples of NSMutableArray being used. Something may become apparent when I really try to use it in anger. Currently my basic binding seems to work (as far as I have tested it).


Comment 5 Jeffrey Stedfast 2011-10-11 10:03:21 UTC
Okay, if you find anything, let us know and we can consider binding NSMutableArray (it probably wouldn't be hard).
Comment 6 xy 2011-11-20 15:01:19 UTC
Hello all, 

I'm binding obj-c methods with specific documentation saying it requires add/remove/move functionality of NSMutableArray. Here's a method example:

+ (id)sectionWithHeaderTitle:(NSString *)sectionHeaderTitle
				   withItems:(NSMutableArray *)sectionItems;

It would be great if we could get this functionality soon.
Comment 7 Jeffrey Stedfast 2011-11-28 18:46:57 UTC
I guess now we have an actual use-case.
Comment 8 Miguel de Icaza [MSFT] 2011-11-30 10:17:27 UTC
Few comments:

In general, do not pass NSArrays unless you have a real reason to do so.   In general, use C# arrays of NSObjects to expose a strongly typed API.   So instead of:

void AddViews (NSArray views);


void AddViews (NSView [] views);

On the particular case of the sample that takes an NSMutableArray, please provide a pointer to the code.

In general, we do not expose NSMutableArray as a design decision.   If an API requires an NSMutableArray, you can use an NSObject as the argument meanwhile and create your own binding, but in general, it means that the library is poorly designed.
Comment 9 xy 2011-12-03 18:48:03 UTC
Not sure by your comment that "the library is poorly designed" if it has methods taking in NSMutableArray, or properties that are NSMutableArray. Isn't this just a list that's updatable, akin to System.Collections.Generic.List in C#? This seems like a common use case to me.
Comment 10 Matt Jones 2012-02-08 10:05:31 UTC
Is there any chance of getting a reason behind the decision to not expose NSMutableArray? I keep getting asked why it's not there.
Comment 11 Alek Slater 2012-02-08 10:14:22 UTC
I'm also quite curious about this, and the argumentation behind why "it means that the library is
poorly designed".

Libraries, Facebook-Connect being one of them, makes use of NSMutableArrays.
Comment 12 Miguel de Icaza [MSFT] 2012-02-08 12:14:58 UTC
The reason why it is poorly designed is because you can alter the state of the collection without the object that exposes the array that the changes took place.

For example, if you had a:

(NSMutableArray *) employees;

You could add and remove employees at will, without the object knowing that you did so.   

A well designed API would expose instead:

- (NSArray *) employees;
- (void) addEmployee: (Employee *);
- (void) removeEmployee: (Employee *);

Both of those would update the internal mutable array and any other links or components that are needed internally.    

That is why no single Apple API exposes NSMutableArray as a return value from a property or method.


That being said, we added NSMutableArray in MonoTouch 5.2.3, but you will get a warning message every time you use it.
Comment 13 Alek Slater 2012-02-08 12:18:52 UTC
yeah, that makes sense :D thx