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

41 Answered Questions

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

91 Answered Questions

33 Answered Questions

[SOLVED] How to change the name of an iOS app?

  • 2008-10-27 03:07:03
  • Robert Gould
  • 436357 View
  • 927 Score
  • 33 Answer
  • Tags:   ios

22 Answered Questions

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

41 Answered Questions

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

9 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

12 Answered Questions

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

  • 2014-06-21 08:31:44
  • Jay Lieske
  • 56392 View
  • 161 Score
  • 12 Answer
  • Tags:   swift

37 Answered Questions

[SOLVED] How to check iOS version?

  • 2010-07-26 23:30:11
  • John
  • 422879 View
  • 810 Score
  • 37 Answer
  • Tags:   ios objective-c

2 Answered Questions

[SOLVED] Check if array contains a specific enum type

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

1 Answered Questions

[SOLVED] Swift Generic enum function to check case

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

Sponsored Content