By Tometoyou


2018-08-10 12:04:08 8 Comments

I have the following enums:

enum BulletinOption {
    case notifications
    case share(type: EventType)
}

enum EventType {
    case singleEvent(position: Int, text: String)
    case multipleEvents(text: String)
}

I create an array of enums like:

var options: [BulletinOption] = [
    .notifications,
    .share(type: .singleEvent(position: 8, text: "My text"))
]

What I want to do is check whether the options array contains the .share enum (doesn't matter about the type associated with it), and replace it with a different type of .share enum.

e.g.

if options.contains(BulletinOption.share) {
    // find position of .share and replace it 
    // with .share(type: .multipleEvents(text: "some text"))
}

How can I do that?

2 comments

@Nirav D 2018-08-10 12:15:37

If you want to access array index and object both then you can use for case with your options array.

for case let (index,BulletinOption.share(_)) in options.enumerated() {
    //Change value here
    options[index] = .share(type: .multipleEvents(text: "some text"))

    //You can also break the loop if you want to change for only first search object
}

@CZ54 2018-08-10 12:07:23

He is the trick :

extension BulletinOption: Equatable {
   static func ==(lhs: BulletinOption, rhs: BulletinOption) -> Bool {
        switch (lhs, rhs) {
        case (.notifications, .notifications):
            return true
        case (.share(_), .share(_)):
            return true
        default:
            return false
        }
    } 

@Tometoyou 2018-08-10 12:12:48

Thanks, how do I use this to check whether the array contains the .share enum case, and the index of the enum in the array?

@CZ54 2018-08-10 12:46:03

you can do myArray.index(of: BulletinOption.share(type: Eventype.smthg))

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to check for an active Internet connection on iOS or macOS?

90 Answered Questions

[SOLVED] How can I make a UITextField move up when the keyboard is present?

38 Answered Questions

[SOLVED] How to check iOS version?

  • 2010-07-26 23:30:11
  • John
  • 412313 View
  • 790 Score
  • 38 Answer
  • Tags:   ios objective-c

8 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

41 Answered Questions

[SOLVED] How to enumerate an enum with String type?

20 Answered Questions

[SOLVED] How do I check if a string contains another string in Objective-C?

1 Answered Questions

[SOLVED] Is it possible to store pattern of enum with associated value in array?

  • 2018-03-21 14:26:50
  • Leo
  • 122 View
  • 5 Score
  • 1 Answer
  • Tags:   swift enums

2 Answered Questions

[SOLVED] Check if array contains a specific enum type

  • 2018-02-08 13:34:52
  • Cory
  • 684 View
  • 0 Score
  • 2 Answer
  • Tags:   ios swift enums

11 Answered Questions

[SOLVED] How to test equality of Swift enums with associated values

  • 2014-06-21 08:31:44
  • Jay Lieske
  • 48519 View
  • 149 Score
  • 11 Answer
  • Tags:   swift

1 Answered Questions

[SOLVED] Swift Generic enum function to check case

  • 2017-03-25 23:06:27
  • HNK
  • 491 View
  • 3 Score
  • 1 Answer
  • Tags:   swift

Sponsored Content