‘Int’ is not a subtype of ‘()’ Swift

The below code throws the error ,’Int’ is not a subtype of ‘()’

func addHelpFunction() -> (Int -> Int){
    func adderfunc() -> Int {
        return 6
    }
    return adderfunc
}

Now, if I parameterise the inner function it works fine

func addHelpFunction() -> (Int -> Int){
    func adderfunc(number: Int) -> Int {
        return 6
    }
    return adderfunc
}

after compiling the code , it throws a new error “failed with exit code 254”

Swift – [Int] is not a subtype of @lvalue$T2

I’m trying to do a simple sort function in a swift playground but I get the following error: Playground execution failed: error: <EXPR>:20:22: error: ‘[Int]’ is not a subtype of ‘@lvalue $T2’ sw



Swift: NSNumber is not a subtype of Float

I tried the code from Swift Programming Language in playground and got the following error NSNumber is not a subtype of Float, I just modified it slightly by making x and y of type Float in struct P

Swift “[T] is not a subtype of”-Error

Running into a bit of a trouble with a Swift tutorial… func exchange<T>(inout data:[T], i:Int, j:Int) { let temp = data[i]; data[i] = data[j]; data[j] = temp; } func swapLeft<T: Comparable&

Swift – ‘Bool’ is not a subtype of ‘Void’?

I’m getting the following error: ‘Bool’ is not a subtype of ‘Void’ performBlock takes a void closure with no argument, and method itself has a single argument, so I should be able to use the following

Swift: NSNumber is not a subtype of UIViewAnimationCurve

How do I get the line below to compile? UIView.setAnimationCurve(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue) Right now, it gives the compile error: ‘NSNumber’ is not a subtype of ‘UIV

Swift: CInt to Int conversion?

What is the most concise way to convert a CInt to an Int (the native integer type) in Swift? Right now I’m using: NSNumber.numberWithInt(myCInt).integerValue Is this the preferred approach?

Swift – Cast Int into enum:Int

I am very new to Swift (got started this week) and I’m migrating my app from Objective-C. I have basically the following code in Objective-C that works fine: typedef enum : int { MyTimeFilter1Hour = 1

c#4.0: int a real subtype of object? covariance, ienumerable and value types

I wonder why IEnumerable<int> can’t be assigned to a IEnumerable<object>. After all IEnumerable is one of the few interfaces that supports covariance… The subtype relation and covarianc

Convert Int to String in Swift

I’m a fairly new programmer – half way through learning Objective-C and now trying to apply that knowledge to Swift. I’m trying to work out how to cast an Int into a String in Swift. I figure out a wo

Swift: why ‘subscript(var digitIndex: Int) -> Int’ is a valid function signature?

This piece of code comes from Swift documentation https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html extension Int { subscript

Answers

Your addHelpFunction() function is returning a function typed as (Int) -> (Int). In your first example, you are returning adderFunc() that is typed as () -> (Int) and thus violates the required return type. In your second example, adderFunc() is typed properly.

Note that a more idiomatic way to implement addHelpFunction() is to return an anonymous closure as:

func addHelpFunction() -> (Int -> Int) {
  return { (n:Int) -> (Int) in return 6 }
}

In action:

 41> var ret6 = addHelpFunction()
ret6: (Int -> Int) =
 42> ret6(10)
$R19: (Int) = 6

Perhaps using the idiomatic closure will avoid the possible compiler error.