0

I recently came across the newest Stanford iOS Programming course given by Paul Hagerty. Yay!

I completed Lesson 01 with no problem. Now, I'm working on Lesson 02.

Having a problem around 42 minutes in when Paul shows how to overload the performOperation function. The idea is to have two definitions: one for a two argument function (+, -, *, /) and the other for a single argument function, e.g. sqrt.

It seems that since the lesson was posted, Swift has been updated/changed and this no longer works as shown in the lesson.

Does anyone know how to make it work? I've searched around and can't find anything useful.

func performOperation (operation: (Double, Double) -> Double) { if operandStack.count >= 2 { displayValue = operation (operandStack.removeLast(), operandStack.removeLast()) enter () } }

func performOperation (operation: Double -> Double) { if operandStack.count >= 1 { displayValue = operation (operandStack.removeLast()) enter () } }

Thanks in advance!

Zonker.in.Geneva
  • 1,389
  • 11
  • 19
  • You should work on your Google Fu :) This has been asked at least 5 times this month (same Stanford course, same code, same error message) – Martin R Apr 28 '15 at 11:59
  • I searched for many different combinations of tags and the exact error message. The link you provided has only one tag (`swift`) and doesn't mention any of the other tags. So, yeah, I didn't find it. So, I have my work-around, thanks. But, I don't feel like the issue is resolved. The prevailing notion is that Objective-C doesn't support overloading and therefore it fails, but then why did it work in an earlier version of Swift? That is left unanswered. Even if the compiler just didn't catch it, it would still crash, right? But, it didn't crash. It worked perfectly in the demo. – Zonker.in.Geneva Apr 28 '15 at 13:46
  • You asked *"Does anyone know how to make it work"* and that *is* answered in that thread (use different method names, or don't inherit from NSObject, or make the method private). – *"It seems that since the lesson was posted, Swift has been updated/changed"* that is also answered in that thread, with references to the release notes. – Martin R Apr 28 '15 at 19:10
  • (cont.) If a feature is not supported then it does not necessarily crash at runtime. It can be "undefined behaviour" and might work by chance, or crash or give wrong results. – But of course it is legitimate to ask *"why did it work in an earlier version of Swift?"*, but that is not what you asked here, so you could post it as a separate question. – Martin R Apr 28 '15 at 19:16

0 Answers0