Skip to content

[objective_c] Collections support for NS[Mutable]Dictionary and NS[Mutable]Set #2234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

liamappelbe
Copy link
Contributor

@liamappelbe liamappelbe commented Apr 24, 2025

NSDictionary and NSMutableDictionary now mix in MapBase, and NSSet and NSMutableSet mix in SetBase. The immutable variants just throw if the mutating methods are called.

The implementation is mostly straightforward. To make iteration easier, I also changed NSEnumerator to implement Iterator. That way, we can use the existing [key/object]Enumerator() methods to iterate the collections without having to add another layer of wrapping.

The one quirk is that NS[Mutable]Dictionary's keys must implement the NSCopying protocol, which means that on the Dart side we have a bunch of NSCopying objects in the API, instead of pure ObjCObjectBase. But the ObjC API isn't very type safe, so there are still a bunch of places where it uses ObjCObjectBase for the keys. So I had to do some castFrom calls, and add a _NSDictionaryKeyIterator that wraps the NSEnumerator and casts the elements as it's iterating.

Also, it turns out if a class extends Base and mixes in Mixin, the methods from Mixin will override any matching methods from Base. This means some of the methods on NSMutableArray were falling back to the slow versions on ListMixin, rather than using the fast versions from NSArray. So I had to duplicate some methods. Same logic applies to NSMutableDictionary and NSMutableSet.

Fixes #1444
Part of #1443

Copy link

github-actions bot commented Apr 24, 2025

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
objective_c Non-Breaking 7.1.0 7.2.0-wip 7.2.0 ✔️
Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

API leaks ⚠️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
objective_c _Version

This check can be disabled by tagging the PR with skip-leaking-check.

License Headers ✔️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/jni/lib/src/third_party/generated_bindings.dart
pkgs/objective_c/lib/src/ns_input_stream.dart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support bracket notation on Obj-C collections
1 participant