Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

MGLStyle.layers and .sources bridge to Swift as untyped collections #7459

Closed

Description

@1ec5

MGLStyle.layers is typed as a read-write NSMutableArray of MGLStyleLayer objects, while MGLStyle.sources is typed as a read-write NSMutableSet of MGLSource objects. Ironically, Swift is unable to distinguish between a read-write NSArray property and a read-write NSMutableArray property (likewise for NSMutableSet), so the properties end up getting bridged as NSMutableArray and NSMutableSet, respectively, instead of [MGLStyleLayer] and [MGLSource].

We should find a way to ensure that Swift developers get the same type safety that Objective-C developers do. One option is to make these collections non-mutable but still implement the mutable KVC methods under the hood. Developers who want to work with the mutable property would then call -mutableArrayValueForKey: or -mutableSetValueForKey:. While workable, that’s hardly discoverable. On the other hand, we explicitly declare a number of non-KVC methods for layers that should reduce the need to work with the mutable array directly in code. Another option would be to define separate layers and mutableLayers properties, but that would double the number of code paths. Any other ideas?

Putting on the v3.4.0 milestone for now to consider our options. Depending on what we think the best option would be, we might be able to postpone this work until a patch release.

/cc @frederoni @incanus @boundsj @bsudekum

Activity

  1. added
    iOS
    macOS
    on Dec 15, 2016
  2. added this to the ios-v3.4.0 milestone on Dec 15, 2016
  3. 1ec5 commented on Dec 18, 2016

    @1ec5
    ContributorAuthor

    The most analogous API in Cocoa, NSView.subviews, is declared as a property of NSArray<__kindof NSView *> * type with copy semantics. We should make the layers property an NSArray and document that it is possible to get a mutable array using -mutableArrayValueForKey:, thanks to the key-value coding work in #6097.

    At the same time, we should use the __kindof keyword with the layers and sources arrays, so that the following cast becomes unnecessary in Objective-C:

    MGLLineStyleLayer *line = (MGLLineStyleLayer *)[style layerWithIdentifier:@"roads"];
  4. boundsj commented on Jan 8, 2017

    @boundsj
    Contributor

    This was fixed by #7539

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

Metadata

Metadata

Assignees

Labels

bugiOSmacOSruntime styling

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions