Swift's pitfall: How to implement multiple selection of enum

Swift's pitfall: How to implement multiple selection of enum

question

In OC, enum can have multiple selections. For example:

  1. NSString* string = @ "a paragraph of text" ;
  2. CGRect boundingRect = [string boundingRectWithSize:CGSizeMake(label.frame.width, CGFloat.max) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];

By combining two enum values ​​with |, you can achieve the effect of multiple selection.

Now the question is: how to achieve the same effect in Swift?

Option Type in Swift

Swift's enum does not support multiple selections, so the option type that supports multiple selections should be implemented using struct: RawOptionSet. To implement the options type yourself, you can refer to this answer.

The official SDK is ported using this method.

Correct way to write

The correct way to write the code in the above example using Swift is as follows:

  1. let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading
  2. let boundingRect = string.bridgeToObjectiveC().boundingRectWithSize(CGSizeMake(label.frame.width, CGFloat.max), options: options, attributes: attributes, context: nil)

In OS X 10.10, this works.

iOS SDK bugs

But in the current version of iOS 8 SDK, this way of writing cannot be compiled. This is because in the iOS 8 SDK, NSStringDrawingOptions has been transplanted to enum: Int instead of struct: RawOptionSet.

How to solve this problem? We can only use OC to workaround.

Solution

Because the options used are the same, I simply wrote this class:

OCUtils.h

  1. @interface OCUtils : NSObject
  2. + (NSStringDrawingOptions)stringDrawingOptions;
  3. @end

OCUtils.m

  1. #import "OCUtils.h"  
  2. @implementation OCUtils
  3. + (NSStringDrawingOptions)stringDrawingOptions{
  4. return NSStringDrawingTruncatesLastVisibleLine |
  5. NSStringDrawingUsesLineFragmentOrigin |
  6. NSStringDrawingUsesFontLeading;
  7. }
  8. @end

Add the following to Bridging-Header.h:

  1. #import "OCUtils.h"  

Swift files used

  1. let boundingRect = string.bridgeToObjectiveC().boundingRectWithSize(CGSizeMake(label.frame.width, CGFloat.max), options: OCUtils.stringDrawingOptions(), attributes: attributes, context: nil)

If there are further requirements, it can be changed to be more general. The overall principle can only be like this.

Reference for this article : How to pass multiple enum values ​​as a function parameter

Original article: Swift's pitfall: How to implement multiple selection of enum

<<:  CES2015: ZTE releases 6-inch ultra-large screen mobile phone Grand X Max+

>>:  Break the 24-hour unspoken rule and create truly innovative apps

Recommend

Sad news: Fat people who exercise can only become a strong fat person

Spring is a season for losing weight. In order to...

The calm before the outbreak? Comprehensive thinking about "Mini Programs"

Since WeChat announced the upcoming release of Mi...

How to prevent cardiovascular and cerebrovascular diseases in summer?

Author: Sun Taixin, Chief Physician, Beijing Elec...

Apple updates the App Store review guidelines again!

As the autumn conference is approaching and the o...

Web caching via HTTP protocol

Dear, do you know what cache is? In fact, caching...

The 2017 Internet Marketing Vocabulary, a must-have for Internet people! !

This is a major update to the internet marketing ...

What else is there in Children's Day marketing besides nostalgia?

Whenever holidays come, brands often have to rack...

There are only a few habits between you and the big guns of operations

It is basically a consensus among everyone that o...

Can a few black dots say goodbye to motion sickness?

Motion sickness, medically known as "motion ...