0

I am using Generics and protocol to make a name space for my Utils Class. and here when I goes to [Array] , I met some problem . here is the code :

in namespce.swift: enter image description here and in Array Extention: enter image description here can any one tell me how can i fix it?


Update: I change my code to : enter image description here

and here I have another problem , I have use "Self" in the function. but the T: Sequence type has no member of "index" .

1
  • It would be better if you can include the actual code instead of images.
    – Kamran
    Nov 28, 2018 at 5:47

1 Answer 1

0

Not sure what you are trying to achieve here (and if it's still relevant) but you should constrain your generic to RangeReplaceableCollection (to be able to use remove(at:)) instead of Sequence. The firstIndex(of:) method (which returns the index of the first matching element) is available on Collection where Element: Equatable.

extension JX_TypeWrapper where T: RangeReplaceableCollection, T.Element: Equatable {
  mutating func remove(object: T.Element) {
    if let index = SELF.firstIndex(of: object) {
      SELF.remove(at: index)
    }
  }
}

Which allows you to wrap an array into your JX_TypeWrapper:

var array: [Int] = [1, 2, 3]
var wrapped = JX_TypeWrapper(value: array)
wrapped.remove(object: 2)
wrapped.SELF // [1, 3]
1
  • thanks , I have tried your function, it worked. But if I just want to use ary.ext.remove(object: 3) what should i do?
    – Neko
    Dec 20, 2018 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.