Bug 52712 - UIContentSizeCategory should be string constants, string values not available !
Summary: UIContentSizeCategory should be string constants, string values not available !
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 10.4 (C9)
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-02-23 18:20 UTC by softlion
Modified: 2017-02-24 18:24 UTC (History)
3 users (show)

Tags:
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:
Status:
RESOLVED ANSWERED

Description softlion 2017-02-23 18:20:22 UTC
UIContentSizeCategory is an enum (int).

UIApplication.SharedApplication.PreferredContentSizeCategory returns an NSString.

You can not create a code like this, it won't work:

                switch (UIApplication.SharedApplication.PreferredContentSizeCategory)
                {
                    case UIContentSizeCategory.AccessibilityExtraExtraExtraLarge:
                       return 23 / 16f;
                ...

In iOS, UIContentSizeCategory are strings.
So PreferredContentSizeCategory should return a UIContentSizeCategory enum, or the enums should be strings.

Is there a workaround ?
Comment 1 Vincent Dondain [MSFT] 2017-02-24 06:17:42 UTC
Hi,

We do bind those constants to enums and have ways to go from enum value to NSString (GetConstant) constant and vice versa (GetValue).

So the code you want to write is:

```
switch (UIContentSizeCategoryExtensions.GetValue (UIApplication.SharedApplication.PreferredContentSizeCategory)) {
            case UIContentSizeCategory.AccessibilityExtraExtraExtraLarge:
                return 23 / 16f;
}
```

Let me introduce you to what we call "smart enums" (which is a pretty new concept for Xamarin.iOS).

I wish the doc was up already on our website but we're having some technical issues right now. It'll be here: https://developer.xamarin.com/guides/cross-platform/macios/binding/binding-types-reference/

Here's the doc about smart enums:

# Enum Attributes

Mapping `NSString` constants to enum values is an easy way to create better .NET API. It:

* allows code completion to be more useful, by showing **only** the correct values for the API;
* adds type safety, you cannot use another `NSString` constant in a incorrect context; and
* allows to hide some constants, making code completion show shorter API list without losing functionality.

Example:

```
enum NSRunLoopMode {

	[DefaultEnumValue]
	[Field ("NSDefaultRunLoopMode")]
	Default,

	[Field ("NSRunLoopCommonModes")]
	Common,
	
	[Field (null)]
	Other = 1000
}
```

From the above binding definition the generator will create the `enum` itself and will
also create a `*Extensions` static type that includes two-ways conversion methods
between the enum values and the `NSString` constants. This means the constants remains
available to developers even if they are not part of the API.

Examples:

```
// using the NSString constant in a different API / framework / 3rd party code
CallApiRequiringAnNSString (NSRunLoopMode.Default.GetConstant ());
```

```
// converting the constants from a different API / framework / 3rd party code
var constant = CallApiReturningAnNSString ();
// back into an enum value
CallApiWithEnum (NSRunLoopModeExtensions.GetValue (constant));
```

Cheers,

Vincent.
Comment 2 softlion 2017-02-24 08:44:04 UTC
Ok nice !
I searched the doc and xamarin decompiled sources yesterday but can't find any example that uses these enums.

Will UIContentSizeCategoryExtensions.GetValue crash on ios 9 as UIContentSizeCategoryExtraSmall is checked first and UIContentSizeCategoryExtraSmall is only available on ios 10 ?

Benjamin.
Comment 3 Vincent Dondain [MSFT] 2017-02-24 18:24:13 UTC
Hey,

So first of all only `UIContentSizeCategoryUnspecified` was introduced in iOS 10. All the other constants (therefore enum values) are iOS 7+.

See:

[Introduced (PlatformName.iOS, 7,0)]
[Unavailable (PlatformName.WatchOS, PlatformArchitecture.All)]
public enum UIContentSizeCategory : int {
	[Introduced (PlatformName.iOS, 10,0)]
	[Introduced (PlatformName.TvOS, 10,0)]
	Unspecified = 0,
	ExtraSmall = 1,
	Small = 2,
	Medium = 3,
	Large = 4,
	ExtraLarge = 5,
	ExtraExtraLarge = 6,
	ExtraExtraExtraLarge = 7,
	AccessibilityMedium = 8,
	AccessibilityLarge = 9,
	AccessibilityExtraLarge = 10,
	AccessibilityExtraExtraLarge = 11,
	AccessibilityExtraExtraExtraLarge = 12,
}

Then to answer your question about the behavior of GetValue instead of using words let me show you the generated code for GetValue ():

https://gist.github.com/VincentDondain/2a92f52e756775fe5d211cdf774aa6f0

and FYI here's the one for GetConstant ()

https://gist.github.com/VincentDondain/3d6695d502eb413e5a3a78389daec805