Since Xcode 7 and Swift 2.0, I get the error above, like in the screenshot shown here:
I got this message when using do-try-catch in a Failable initializer:
public init?() {
do {
...
super.init(superParam: try getParamForSuper())
...
} catch {
...
}
}
The compilation succeeded when moving the try call to it's own local variable:
public init?() {
do {
...
let superParam = try getParamForSuper()
super.init(superParam: superParam)
...
} catch {
...
}
}
My case, Swift 5.1, Xcode 10.3 (10G8)
Code was crashing Swift when a nested function was using the argument of an outer function as a default parameter.
e.g.
func foo(duration: TimeInterval) {
func bar(duration: TimeInterval = duration) {
}
}
I hope this helps.
To me what caused this error was:
I created a file to create extensions on UIView. Inside this file, I created a private protocol named Foo
.
Then I made:
extension UIView: Foo
Removing the private from the protocol made the error go away.
I guess this is probably a bug. The compiler should warn us about the issue. The same way it warns us we can't add private conformances to types it should tell us that conformance should be using a "public/internal" protocol.
I didn't try other solutions. I got this problem for this setup:
func speacialAdd(_ num1: Int, to num2: Int){
func specialMultiply(_ digit1: Int, with digit2: Int = num2){ // SOURCE OF PROBLEM
print(digit2)
print(digit1)
}
specialMultiply(5)
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(speacialAdd(5, to: 6))
}
}
This line is the source of problem. Defaulting it to a argument seems to not work for a nested function
func specialMultiply(_ digit1: Int, with digit2: Int = num2) // ERROR
Solutions are:
func specialMultiply(_ digit1: Int, with digit2: Int) // OK
func specialMultiply(_ digit1: Int, with digit2: Int = 6) // OK
FWIW I actually first wrote this in playground and got a different error:
Playground execution failed:
error: Couldn't lookup symbols:
__T013__lldb_expr_111speacialAddySi_Si2totF4num2L_Sifau
In my case,
The compiler would give me the message:
Incorrect number of arguments passed to called function!
%4 = call %swift.type* @_T015SimplifiedCoder6StructVMa() #1, !dbg !3112
<unknown>:0: error: fatal error encountered during compilation; please
file a bug report with your project and the crash log
<unknown>:0: note: Broken function found, compilation aborted!
but I realized that I missed a default generic parameter:
class Class<K> {
init<T: Protocol>(_ value: T) where T.Key == K {}
}
protocol Protocol {
associatedtype Key
static func getClass<NewKey>(_: NewKey.Type) -> Class<NewKey>
}
struct Struct<K>: Protocol {
typealias Key = K
static func getClass<NewKey>(_: NewKey.Type) -> Class<NewKey> {
let _self = Struct<NewKey>()
return Class(_self)
}
}
protocol CanGetClass {
associatedtype StructType: Protocol
}
extension CanGetClass {
func getClass<Key>(_: Key.Type) -> Class<Key> {
return StructType.getClass(Key.self)
}
}
struct R: CanGetClass {
typealias StructType = Struct
}
changed:
typealias StructType = Struct
to:
typealias StructType = Struct<Int>
the extension of CanGetClass tried to call getClass on an incomplete type.
This issue is still present in Xcode 10.2.1. After cleaning the build folder, the error went away.