Xamarin-macios: [Proposal] Generic NSObject wrapper

Created on 18 Apr 2020  路  3Comments  路  Source: xamarin/xamarin-macios

Something like this:

class NS<T> : NSObject
{
    public NS(T value)
    {
        Value = value;
    }

    public T Value { get; }
}

For more convenient using:

  • UICollectionViewDiffableDataSource:
new UICollectionViewDiffableDataSource<NS<MySectionEnum>, NS<int>>(...);
  • NSDiffableDataSourceSnapshot<,>

Sample

var dataSource = new UICollectionViewDiffableDataSource<NS<MySectionEnum>, NS<int>>(...);

var snapshot = new NSDiffableDataSourceSnapshot<NS<MySectionEnum>, NS<int>>();

snapshot.AppendSections(new[] { new NS<MySectionEnum>(section); });

var items = Enumerable.Range(0, 100).Select(x => new NS<int>(x)).ToArray();
snapshot.AppendItems(items);

dataSource.ApplySnapshot(snapshot, animatingDifferences: false);

Most helpful comment

In the above case you better use the existing NSNumber type and avoid creating a custom type (in addition to the generic extra cost).

All 3 comments

Possible problem of this approach: https://github.com/xamarin/xamarin-macios/issues/6289

In the above case you better use the existing NSNumber type and avoid creating a custom type (in addition to the generic extra cost).

@wcoder - Thanks for the bug report.

As noted by @Therzok, we'd rather not push people to using generic NSObject due to the non-trivial performance penalty. In some cases you can use existing NS types, such as NSNumber, and in others you'll need a hand rolled type.

If we bake in such a wrapper, I fear we'll push people to using it in cases they shouldn't.

Due to that, we're going to reject this proposal. Thanks for the idea though!

Was this page helpful?
0 / 5 - 0 ratings