Overloaded method bug?

Discussion in 'iOS Programming' started by Mork, Apr 10, 2015.

  1. Mork, Apr 10, 2015
    Last edited by a moderator: Apr 10, 2015

    Mork macrumors 6502

    Joined:
    Jan 9, 2009
    #1
    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]
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    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.
     
  3. Mork thread starter macrumors 6502

    Joined:
    Jan 9, 2009
    #3
    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
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. Mork thread starter macrumors 6502

    Joined:
    Jan 9, 2009
    #5

    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,
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. Mork thread starter macrumors 6502

    Joined:
    Jan 9, 2009
    #7
    What do you think I was doing? .... ;)

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

    Thanks,
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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.
     
  9. Mork thread starter macrumors 6502

    Joined:
    Jan 9, 2009
    #9
    I totally appreciate all your replies.

    Thanks,
     
  10. xStep, Apr 10, 2015
    Last edited: Apr 14, 2015

    xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #10
    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.
     
  11. Mork thread starter macrumors 6502

    Joined:
    Jan 9, 2009
    #11
    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
     
  12. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #12
    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?
     
  13. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #13
    Played a little more and got positive results for the instructor format.

    So, on exactly which line is that error being reported on?
     
  14. mixth macrumors newbie

    Joined:
    Apr 13, 2015
    Location:
    Bangkok, Thailand
    #14
    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/...ror-which-i-dont-understand/29457777#29457777

     
  15. felipeandrade macrumors newbie

    Joined:
    Apr 22, 2015
    #15
    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.
     
  16. acarlosportela macrumors newbie

    Joined:
    Jun 20, 2015
    #16
    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.
     

Share This Page