2

I'm trying to extend Array<T> in Swift to add a function that calls self.append. The problem is that Swift won't seem to let me do that - when I try the following code:

extension Array { mutating func AppendObj<T>(obj: T) { //... self.append(obj); } } 

it says 'T' is not convertable to 'T,' which I assume means that the 'T' the underlying Array is using might be different than the T passed to AppendObj. It also won't allow me to use extension Array<T> ("use of undeclared type T").

Is it possible to extend & use generic structs/methods in Swift? Thanks!

1
  • If I may make a suggestion, don't call your method AppendObj. Instead, call it appendObj. Method names should begin with a lower case letter in Swift (and Obj-C). Commented Dec 21, 2014 at 20:05

1 Answer 1

2

You shouldn't specify generics for the function, it overrides the existing template. Just leave it out:

extension Array { mutating func AppendObj(obj: T) { //... self.append(obj); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

That...makes perfect sense xD Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.