UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to append value of NSSet.Element into an array? swift

Forums > Swift

How to append value of NSSet.Element into an array? I got this error (Cannot convert value of type '[NSSet.Element]' (aka 'Array<Any>') to expected argument type 'Int')

func unique(_ arr: [Int]) -> [Int] {

    var arr0 = [Int]()

    let countedSet = NSCountedSet(array: arr)
    var singleOccurrencies = countedSet.filter { countedSet.count(for: $0) == 1 }

    arr0.append(singleOccurrencies)  // ERROR here
    print(arr0)

    return arr0
}

print(unique([ 4 ,3 ,-5 ,4 ]))

2      

does this work?

Not familiar with countedSets but this certainly stops the compiler complaining so that's got to be a step in the right direction I'd hope :-)

I get a result of [3, -5] for this

    func unique(_ arr: [Int]) -> [Int] {

          var arr0 = [Int]()

          let countedSet = NSCountedSet(array: arr)
         if let singleOccurrencies = countedSet.filter({ countedSet.count(for: $0) == 1 }) as? [Int] {
            arr0.append(contentsOf: singleOccurrencies)
         }
            print(arr0)

          return arr0
      }

3      

yes it workes, thank you

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.