0

I’m trying to pass multiple options for NSLineBreakMode in Swift. In Objective C this works:

label.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail;

Referring to this, I’ve tried setting options in a constant like this:

var lineBreakOptions: NSLineBreakMode = [.ByWordWrapping,.ByTruncatingTail]
passageExcerpt.lineBreakMode = lineBreakOptions

But I’m getting an error back that says:

Contextual type 'NSLineBreakMode' cannot be used with array literal.

Is there a way to pass multiple options for NSLineBreakMode’s enum?

Community
  • 1
  • 1
noahread
  • 25
  • 5

1 Answers1

2

NSLineBreakMode is an enum type rather than an OptionSet type (even in Objective-C). You can assign only one case.

In Objective-C the expression

 NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail

works, but it sets the mode only to NSLineBreakByTruncatingTail by "or-ing" the raw values 0 and 4, check this

NSLog(@"%ld", NSLineBreakByWordWrapping); // 0
NSLog(@"%ld", NSLineBreakByTruncatingTail); // 4
NSLog(@"%ld", NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail); // 4

So the Swift equivalent is just

label.lineBreakMode = .ByTruncatingTail
vadian
  • 274,689
  • 30
  • 353
  • 361