2

I want to create a method that takes in one or more parameters but when I try the following code I get the following error message.

struct SomeStruct {
    static func method(arrays: AnyObject[]...) -> AnyObject[] {
        return []
    }
}
SomeStruct.method([1], [2])


Cannot convert the expression's type 'AnyObject[]' to type 'IntegerLiteralConvertible'

If I run the following code

SomeStruct.method(["1"], ["2"])

I get the following error

Cannot convert the expression's type 'AnyObject[]' to type 'ExtendedGraphemeClusterLiteralConvertible'

I want to allow 0 or more array of items to be passed into this method of any element type including numbers. How can I do this?

enter image description here

3
  • This code compiles and runs fine for me...If you're in the playground, try relaunching it. I've hit some bugs that left it wacky. Commented Jun 5, 2014 at 0:53
  • Works fine in the Playground Commented Jun 5, 2014 at 0:55
  • Attaching screenshot of the error I get in Playground. I am running Mac OS X Yosemite. Not sure if this is a bug. Commented Jun 5, 2014 at 2:02

1 Answer 1

1

I was able to get it to work by using a generic instead of AnyObject.

struct SomeStruct {
    static func method<T>(arrays: T[]...) -> T[] {
        return []
    }
}
SomeStruct.method([1], [2])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.