Overloaded method bug?

Mork

macrumors 6502
Original poster
Jan 9, 2009
422
14
I'm using xCode 6.3 and following along with the Stanford Swift programming online (free) class, I typed in the same code as the instructor to implement the square root function.

Yet, although the code (below) looks identical to what works for him, I'm getting a "Method 'performOperation' with Objective-C selector 'performOperation:' conflicts with previous declaration with the same Objective-C selector."

Confusing since the argument lists are clearly different.

Of course this is a swift project and all the swift code has been working up to this point so I'm a bit confused.

Is this a known bug or do I have a syntax issue below which is preventing the simple function overloading to work?

Thanks in advance,

- m

Code:
[COLOR="Blue"] func performOperation[B](operation: (Double, Double) -> Double)[/B]
    {
        if (operandStack.count >= 2)
        {
            displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
            enter()
        }
        
    }
    
    // overloaded performOperation for square root which only has one argument.
    func performOperation[B](operation: Double -> Double)[/B]
    {
        if (operandStack.count >= 1)
        {
            displayValue = operation(operandStack.removeLast())
            enter()
        }
    }[/COLOR]
 
Last edited by a moderator:

larswik

macrumors 68000
Sep 8, 2006
1,552
11
You have 2 functions that have the exact same name. Although your arguments are different the names are identical. I am learning swift too but I would think that would cause an issue.

I could be wrong but that is my first thought.
 

Mork

macrumors 6502
Original poster
Jan 9, 2009
422
14
You have 2 functions that have the exact same name. Although your arguments are different the names are identical. I am learning swift too but I would think that would cause an issue.

I could be wrong but that is my first thought.
That's exactly what function overloading is: same name, but different argument lists. It gives you the ability to call the same function (name) without having to create separate functions just because your arguments are different.

Check out the extremely cool (and free) Stanford iOS class. You'll see on Day 2 where the instructor creates the working example of the same function with different argument lists.

Except, his works and mine doesn't.

Not sure why, hence my posting here. :)

https://itunes.apple.com/us/course/developing-ios-8-apps-swift/id961180099

Thanks,

- m
 

chown33

Moderator
Staff member
Aug 9, 2009
8,380
4,367
Pumpkindale
Code:
func performOperation(operation: (Double, Double) -> Double)
func performOperation(operation: Double -> Double)
I'm not sure your examples have different argument lists. Parentheses around a comma-separated series means "tuple" in Swift:
http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

Your 1st func has a single parameter consisting of a tuple that contains two unnamed Doubles.

Your 2nd func also has a single parameter, but it consists of a single Double.

Two functions with one parameter for both might be a problem. In C++, overloading takes type into account. I don't know what Swift does.
 

Mork

macrumors 6502
Original poster
Jan 9, 2009
422
14
Code:
func performOperation(operation: (Double, Double) -> Double)
func performOperation(operation: Double -> Double)
I'm not sure your examples have different argument lists. Parentheses around a comma-separated series means "tuple" in Swift:
http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

Your 1st func has a single parameter consisting of a tuple that contains two unnamed Doubles.

Your 2nd func also has a single parameter, but it consists of a single Double.

Two functions with one parameter for both might be a problem. In C++, overloading takes type into account. I don't know what Swift does.

Interesting.

My code "looks" (unless I've missed something) exactly like what the instructor did - except his works. And, I'm getting an "objective-c" exception even though it says it's Swift.

If you look at the second lecture around time 40 - 45 minutes, you'll see this code.

I have no idea. (that's why I was taking the course....) :)

I took a screenshot of his code, but don't see any way to paste it here...

Thanks,
 

Mork

macrumors 6502
Original poster
Jan 9, 2009
422
14
Read this linked post.

You could type in his code verbatim. Be sure to double-check what you typed; accuracy is important.

I'm not in a position to look at any videos.
What do you think I was doing? .... ;)

Unless I made a mistake typing verbatim, which I don't see....again, hence my posting.

Thanks,
 

chown33

Moderator
Staff member
Aug 9, 2009
8,380
4,367
Pumpkindale
What do you think I was doing? .... ;)

Unless I made a mistake typing verbatim, which I don't see....again, hence my posting.

Thanks,
I'm not sure if you were typing verbatim or not. For example, you posted this:
Code:
func performOperation(operation: Double -> Double)
But according to this Swift reference, a function definition that returns a value should begin like this:
Code:
func sayHello(personName: String) -> String {
In this example the parameter and the returned value are String rather than Double, but I don't think that should affect the syntax. For example, here's a tutorial that shows:
Code:
  func calcTipWithTipPct(tipPct: Double) -> Double {
The syntax of these two are consistent, despite the difference in type.

Your posted code differs in the location of the -> Double denoting the returned type. Yours is inside the parens; the reference and tutorial show them outside the parens.

As I wrote before, I don't know Swift. I only have the references and tutorials and such for syntax. Based on those, your posted code doesn't look quite right. But I can't tell you what it means, because I don't know Swift. Maybe your posted code means the same thing, or maybe it doesn't. That's why I suggested double-checking what you typed.

I also gave a link to a post that explains how to attach images to your posts here. Maybe you can post the screen shot from your video tutorial.
 

Mork

macrumors 6502
Original poster
Jan 9, 2009
422
14
I'm not sure if you were typing verbatim or not. For example, you posted this:
Code:
func performOperation(operation: Double -> Double)
But according to this Swift reference, a function definition that returns a value should begin like this:
Code:
func sayHello(personName: String) -> String {
In this example the parameter and the returned value are String rather than Double, but I don't think that should affect the syntax. For example, here's a tutorial that shows:
Code:
  func calcTipWithTipPct(tipPct: Double) -> Double {
The syntax of these two are consistent, despite the difference in type.

Your posted code differs in the location of the -> Double denoting the returned type. Yours is inside the parens; the reference and tutorial show them outside the parens.

As I wrote before, I don't know Swift. I only have the references and tutorials and such for syntax. Based on those, your posted code doesn't look quite right. But I can't tell you what it means, because I don't know Swift. Maybe your posted code means the same thing, or maybe it doesn't. That's why I suggested double-checking what you typed.

I also gave a link to a post that explains how to attach images to your posts here. Maybe you can post the screen shot from your video tutorial.
I totally appreciate all your replies.

Thanks,
 

xStep

macrumors 68010
Jan 28, 2003
2,001
97
Less lost in L.A.
YOU CAN IGNORE THIS RESPONSE:

I played with your function definitions in Playground and came up with the below setup. I haven't watched the video you are referring to, but there seems to be confusion about how to set up the function. Notice how I've moved the last parenthesis to the left. That one ends the definition of what is being passed to the function. After that, you define the return type. If you are returning a single type then apparently you don't need parenthesis around the type, but if you return a tuple, you would.

Code:
func performOperation(operation: (Double, Double)) -> Double
{
    return operation.0 * operation.1
}

func performOperation(operation: Double) -> Double
{
    return operation * 2
}

Returning a tuple.
Code:
func performOperation2(operation: (Double, Double)) -> (Double, Double)
{
    return (operation.0 * 2, operation.1 * 3)
}
P.S. I just looked at the video and it does have what you say it does. The call to the functions are not using parenthesis either but the curly braces.
 
Last edited:

Mork

macrumors 6502
Original poster
Jan 9, 2009
422
14
I played with your function definitions in Playground and came up with the below setup. I haven't watched the video you are referring to, but there seems to be confusion about how to set up the function. Notice how I've moved the last parenthesis to the left. That one ends the definition of what is being passed to the function. After that, you define the return type. If you are returning a single type then apparently you don't need parenthesis around the type, but if you return a tuple, you would.

Code:
func performOperation(operation: (Double, Double)) -> Double
{
    return operation.0 * operation.1
}

func performOperation(operation: Double) -> Double
{
    return operation * 2
}

Returning a tuple.
Code:
func performOperation2(operation: (Double, Double)) -> (Double, Double)
{
    return (operation.0 * 2, operation.1 * 3)
}
P.S. I just looked at the video and it does have what you say it does. The call to the functions are not using parenthesis either but the curly braces.
What may be confusing here is that this code is using a closure. I'm still not sure why the author's code works and mine doesn't. I think I need to find a mac programming forum.

Thanks,

- m
 

xStep

macrumors 68010
Jan 28, 2003
2,001
97
Less lost in L.A.
Ah, the context of this is at video mark 35:30 to 38 minutes. So what I posted previously doesn't seem to apply here.

Perhaps this Swift syntax has changed since the video was recorded?

One curiosity to me is why your error message refers to Objective-C instead of swift. Are you sure your project is setup as Swift?
 

mixth

macrumors newbie
Apr 13, 2015
1
0
Bangkok, Thailand
I also got the error after follow this e-learning video.

There was a guy made a comment in this facebook post:
https://www.facebook.com/CS193p/posts/644224612371874

which lead to this StackOverflow answer:
http://stackoverflow.com/questions/29457720/swift-compiler-error-which-i-dont-understand/29457777#29457777

Objective-C does not support method overloading, you have to use a different method name. When you inherited UIViewController you inherited NSObject and made the class interopable to Obj-C. Swift on the other hand supports overloading, that's why it works when you remove the inheritance.
 

felipeandrade

macrumors newbie
Apr 22, 2015
1
1
Paul Hegarty was using an early version of Swift, probably 1.0.

The newest version 1.2 introduced a checking type-based overloading of @objc methods and initializers, something not supported by Objective-C. To avoid the issue you can adjust the access control to private then it will hide the implementation details.

Note you can use the exact same definition in your custom classes, the instructor was using it in a subclass of UIViewController.
 
  • Like
Reactions: engram

acarlosportela

macrumors newbie
Jun 20, 2015
1
0
It might be due to this new version. I did this same exercise back in March and had no errors. Now I decided to do it from scratch and got this overloading error.