Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple enum values as a function parameter

Tags:

swift

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]; 
like image 902
devmiles.com Avatar asked Jun 05 '14 15:06

devmiles.com


People also ask

Can enums have multiple values?

Learn to create Java enum where each enum constant may contain multiple values. We may use any of the values of the enum constant in our application code, and we should be able to get the enum constant from any of the values assigned to it.

Can enum be parameterized?

We have to create parameterized constructor for this enum class. Why? Because as we know that enum class's object can't be create explicitly so for initializing we use parameterized constructor. And the constructor cannot be the public or protected it must have private or default modifiers.

Can enum have multiple values C++?

An enum instance may hold multiple values only as much as an int can: that is, not at all.

Can enum store multiple variables?

The Enum constructor can accept multiple values.


2 Answers

Edit: In Swift 3.0:

let options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading] 

Edit: This is how you would use the options enum in Swift 2.0:

let options: NSStringDrawingOptions = [.UsesLineFragmentOrigin, .UsesFontLeading] 

Edit: The issue has been resolved in iOS 8.3 SDK Beta 1 (12F5027d):

Modified NSStringDrawingOptions [struct]

  • From: enum NSStringDrawingOptions : Int
  • To: struct NSStringDrawingOptions : RawOptionSetType

You can now write:

let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading 

After some research and and @Anton Tcholakov's "comment":

  1. If you're targeting OS X 10.10, this is as simple way to do it:

    let size = CGSize(width: 280, height: Int.max) let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading  let boundingRect = string.bridgeToObjectiveC().boundingRectWithSize(size, options: options, attributes: attributes, context: nil) 
  2. However, in iOS 8 SDK (in the current seed), there's a bug, where NSStringDrawingOptions is ported to Swift as enum : Int, instead of struct : RawOptionSet. You should send a bug report to Apple describing this serious problem.

like image 65
akashivskyy Avatar answered Oct 01 '22 22:10

akashivskyy


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) 
like image 22
Alexey Globchastyy Avatar answered Oct 01 '22 21:10

Alexey Globchastyy