user470763
user470763

Reputation:

Unary operator '~' cannot be applied to an operand of type 'MyType'

I have some code that has broken since migrating to Swift 2.0. I now get the error in the title of the question.

message.flags = NSNumber(integer:(MCOMessageFlag(rawValue: message.flags.integerValue).intersect(~MCOMessageFlag.Seen)).rawValue)

MyType is a bitmask. Any idea what has changed in Swift to make this now produce an error?

Edit:

    typedef NS_OPTIONS(NSInteger, MCOMessageFlag) {
    MCOMessageFlagNone          = 0,
    /** Seen/Read flag.*/
    MCOMessageFlagSeen          = 1 << 0,
    /** Replied/Answered flag.*/
    MCOMessageFlagAnswered      = 1 << 1,
    /** Flagged/Starred flag.*/
    MCOMessageFlagFlagged       = 1 << 2,
    /** Deleted flag.*/
    MCOMessageFlagDeleted       = 1 << 3,
    /** Draft flag.*/
    MCOMessageFlagDraft         = 1 << 4,
    /** $MDNSent flag.*/
    MCOMessageFlagMDNSent       = 1 << 5,
    /** $Forwarded flag.*/
    MCOMessageFlagForwarded     = 1 << 6,
    /** $SubmitPending flag.*/
    MCOMessageFlagSubmitPending = 1 << 7,
    /** $Submitted flag.*/
    MCOMessageFlagSubmitted     = 1 << 8,
};

Upvotes: 0

Views: 506

Answers (2)

user470763
user470763

Reputation:

Although Rob's answer worked I thought I should post this too. I also posted the question the the Apple Dev Forums and received a response from Chris Lattner.

As of swift 2, option sets are now set-like, which means you cannot invert them with ~

So instead of intersect() I can use subtractInPlace().

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385720

Considering you just want to end up with an integer anyway:

myObject.prop = NSNumber(integer:
    myObject.prop.integerValue & ~MCOMessageFlag.Seen.rawValue)

Or maybe this would be clearer for your specific case:

if myObject.prop.integerValue == MCOMessageFlag.Seen.rawValue {
    myObject.prop = NSNumber(integer: MCOMessageFlag.None.rawValue)
}

Upvotes: 1

Related Questions