How to pass multiple enum values as a function parameter

enum with multiple values c#
c# compare multiple enum values
c# enum parameter
c# enum multiple attributes
c# enum different values
c# enum with multiple string values
c++ enum multiple values
c# enum multiple names same value

How would I do the following - passing two NSStringDrawing options as a function parameter in swift:

CGRect boundingRect = [string boundingRectWithSize:CGSizeMake(280.0, NSIntegerMax)
                                                      options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                   attributes:options context:nil];

How to pass multiple Enum values in C#, Well, you have to understand what an enum is, in C#. It's basically an integer but with limitations. So when you create an enum like [code ]enum Day {Sat, Sun,� Use the Enum.GetNames (typeof (e)) method, this will return an array of strings with the names. You can also use Enum.GetValues to obtain the counterpart values.

Updated answer for Xcode 6.3:

in Xcode 6.3 Beta (Swift 1.2) this is finally fixed, you can do it now like this:

let boundingRect = "string".boundingRectWithSize(size, options: .UsesLineFragmentOrigin | .UsesFontLeading, attributes:nil, context:nil)

For old version:

It looks like a bug in current beta, for now I write Objective-C method and use it from Swift:

+ (NSStringDrawingOptions)combine:(NSStringDrawingOptions)option1 with:(NSStringDrawingOptions)option2
{
    return option1 | option2;
}

and call form Swift:

let boundingRect = "string".boundingRectWithSize(size, options: StringDrawingOptions.combine(.UsesLineFragmentOrigin, with: .UsesFontLeading), attributes:nil, context:nil)

How to Accept Multiple Enum Values in a Property or Method , So that means that inside our method, we simply need to perform a binary AND on the input parameter and every possible enum value to see if it� When you define the enum, just attribute it with [Flags], set values to powers of two, and it will work this way. Nothing else changes, other than passing multiple values into a function. For example: [Flags] enum DaysOfWeek { Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8, Thursday = 16, Friday = 32, Saturday = 64 } public void RunOnDays(DaysOfWeek days) { bool isTuesdaySet = (days & DaysOfWeek.Tuesday) == DaysOfWeek.Tuesday; if (isTuesdaySet) //

Another solution, use unsafeBitCast.

Like below:

let options = unsafeBitCast(NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue | 
                            NSStringDrawingOptions.UsesFontLeading.rawValue,
                            NSStringDrawingOptions.self)

How to pass multiple enum values as a function par, How to pass multiple enum values as a function par do the following - passing two NSStringDrawing options as a function parameter in swift: Cycling through (looping around) enum lists. In a regular method call, we cannot pass a Type as a function parameter and to be honest, my need to do this has been quite rare.

In Swift 2.1 we may use,

calculatedRect = (text as NSString).boundingRectWithSize(bounds.size, options: NSStringDrawingOptions([.UsesLineFragmentOrigin, .UsesFontLeading]), attributes: [NSFontAttributeName : font], context: nil)

to pass multiple enum values for a parameter.

How to use Enum in C# - CSharp, The main advantage of Enum is make it easy to change values in the future, also you can reduces errors This method requires two parameters. The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. Once it's created, you can then use this enum on as many parameters as you'd like by simply casting the parameter value to an enum, as shown below. function New-Server { param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [ServerType]$Type ) ## Do stuff to create a server here }

in swift 2.0 that will be something like

theStringDrawingOptions = [NSStringDrawingOptions.UsesLineFragmentOrigin,
                           NSStringDrawingOptions.UsesFontLeading]

see lecture wwdc 2015 -> 106_hd_whats_new_in_swift in 6 min

Java Enums: How to Make Enums More Useful, Enums can contain multiple constructors. We can also provide various values as enum-member variables with the public static void main(String[] args) { Because of declaring abstract method at the enum level; all of the� When you define the enum, just attribute it with [Flags], set values to powers of two, and it will work this way. Nothing else changes, other than passing multiple values into a function. For example:

Enum.HasFlag(Enum) Method (System), Parameters. flag: Enum. An enumeration value. Returns. Boolean. true if the bit� Passing textarea, text values to popup ; Help on getting 2 lowest values in an array. population program ; Passing Parameters to Linked Subreports ; Saxon's DocumentBuilder.build throws a NullPointer when passing a stream source ; eliminate duplicate items in combobox! pls help! string enum in c# ; object as parameter vs object method

aspnet mvc pass enum flag to action as parameter � GitHub, //http://john.katsiotis.com/blog/asp.net-mvc---passing-a-flag-enum-to-an-action //Comma-separated Enum values or the equivalent numeric value, your choice! The input parameter for the function can be either an enum object, enum name as string, or the enum as a type. To accomplish this, requires the use of the -Is and the -As type operators. Using Enums with the -Is Type Operator

Enum Types (The Java™ Tutorials > Learning the Java Language , The variable must be equal to one of the values that have been predefined for it. static void main(String[] args) { EnumTest firstDay = new EnumTest(Day. values method that returns an array containing all of the values of the enum in the the Java language does not support multiple inheritance of state (see Multiple � Enums can be marked with the [Flags] attribute which marks the enum as a bit wise enum. You can therefore pass multiple values with an ‘or' | between each enumeration value. Days meetingDays = Days.Tuesday | Days.Thursday; And check for equality with an ‘and' operator. var test = (meetingDays & Days.Thursday) == Days.Thursday;

Comments
  • what sort of a drawing options that would return? ) there's only individual ones, not their combination, so the result of OR operation wouldn't pass the "type check" i guess
  • Sorry, those should be 2 different options. But it wouldn't work neither. This is a bug.
  • @devmiles.com Looks like a bug in iOS SDK, definetely. :( Check out my updated answer.
  • Unfortunately yes. My bug report is still open.
  • In Swift 2.0 this is no longer true. Now most of enums conform to OptionSetType, now you pass the enum values with the same syntax of arrays [.UsesLineFragmentOrigin, .UsesFontLeading]. See: stackoverflow.com/questions/30761996/…
  • I wish I could upvote this twice. I would never have guessed that you could take an arbitrary integer from Objective-C, call it an NSStringDrawingOptions, and get it past the Swift compiler.
  • I ended up making a constant in an Objective-C file and just referencing it from Swift. Hopefully they fix this soon.
  • Amazingly, this bug still exists (iOS 8.1, Xcode 6.1GM2).
  • Actually, iOS SDK 8.3, not Swift 1.2 fixed this. ;)
  • well, now you got your 50 points ) i'm using ios though and still having problems (
  • Not that familiar with Cocoa APIs if I'm honest. I just have the above in a playground and it seems to work. Maybe someone else can help further.
  • No, that doesn't compile on iOS. On iOS, it isn't a RawOptionSet. That's exactly the problem.
  • unfortunately that doesn't work because: Could not find an overload for '|' that accepts the supplied arguments
  • not sure what you've edited, the code itself has errors but the major flow is that | doesn't work with enum : Int