可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Since Xcode 7 and Swift 2.0, I get the error above, like in the screenshot shown here:
I have no idea where this is coming from, cleaning and deleting derived data didn't work.
Anyone else experiencing this problem?
Project settings:
Target settings:
回答1:
Go to project Build settings -> Swift Compiler - code generation -> Optimization Level
-> For both Debug & Release select option "Fast,Single-File Optimaiztion[-O]
回答2:
I have the same problem with all Xcode 6.3 projects, I open in Xcode 7.0.
I created a new project, copied all my source files and resources and everything worked without this compiler error.
I thought this has something to do with the project settings.
I turned off the Swift compile Optimization to "none" and the Trap 6 was gone away. Perhaps there are other settings, which also generate trouble, but for me this it was.
回答3:
In my case
Error
override func observeValueForKeyPath(keyPath: (String!)?, ofObject object: (AnyObject!)?, change: ([NSObject : AnyObject]!)?, context: UnsafeMutablePointer<Void>)
Ok
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>)
回答4:
I got this error too on XCode 7 Beta 5. After I clean the build, then I got another error saying one of my class not conforming to the protocol I just changed. After I fix the issue, it builds. The protocol changes I made is to change two parameter type of a method from Int
to Int32
回答5:
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.
回答6:
I received this when did that:
protocol ProtocolA {
associatedtype BType: ProtocolB
}
protocol ProtocolB {
associatedtype AType: ProtocolA
}
回答7:
This worked for me, so just give it a try. i got this error while converting the code from swift 3 to swift 4.
Simply, go to Project>Target>Build Setting and search 'Swift Compiler - Code Generation' and set Optimization level to 'No Optimazation[-Onone]'.
回答8:
Make sure you don't implement a private protocol into class extension. Most of the time would be quite strange to have a private protocol, but not necessary, depending on what you wish to encapsulate.
Something like, in the same file:
class C: ... {
}
extension C: P {
}
private protocol P: class {
}
Do this an you'll surely get Command failed due to signal: Abort trap: 6
Instead remove the private
modifier from the protocol and the error is fixed.
回答9:
In my case, it was with setting a value to a parameter in a function to nil that was causing the error.
Before:
public func comparableValidator<T: Comparable>(minValue : T? = nil, maxValue : T? = nil, value: T) -> Void {
if let min = minValue {
_assertFunc(min <= value, "\(value) must be at least \(min)")
}
if let max = maxValue {
_assertFunc(max >= value, "\(value) must be at most \(max)")
}
}
After:
public func comparableValidator<T: Comparable>(minValue : T?, maxValue : T?, value: T) -> Void {
if let min = minValue {
_assertFunc(min <= value, "\(value) must be at least \(min)")
}
if let max = maxValue {
_assertFunc(max >= value, "\(value) must be at most \(max)")
}
}
回答10:
I am able to reproduce this simply and consistently with a brand-new project created in Xcode 7.0 beta (7A120f). Note that the problem is likely more broad than the example, but this is 100% reproducible for me by only adding a single line to a new project. This problem also appears to exclusively apply to iOS, not OS X, at least for this example. Have submitted bug report # 21376523 to Apple.
Create a brand-new project in Xcode 7.0 (7A120f). Type is "Game", Language is "Swift", Game Technology is "SceneKit".
Build and run with default settings. Project builds and runs fine in simulator (you will see the default rotating 3D spaceship model).
Add a single SCNVector3 property to GameViewController.swift, like this:
class GameViewController: UIViewController {
var p = SCNVector3Zero
--> Produces "Abort trap: 6". Project will no longer compile.
Change the constant to an empty initializer.
class GameViewController: UIViewController {
var p = SCNVector3()
--> Same problem, "Abort trap: 6"
- Remove property, restoring to the clean project state.
--> "Abort trap: 6" is gone, project again compiles and runs.
Change "var" to "let".
class GameViewController: UIViewController {
let p = SCNVector3Zero
-- > Compiles and runs.
Change property type to SCNVector4 instead of SCNVector3.
class GameViewController: UIViewController {
var p = SCNVector4Zero
-- > Compiles and runs.
EDIT: This problem is fixed in Xcode 7.0 beta-2 (7A121l), if you are getting "Abort trap: 6" due to using a float 3 vector type (such as SCNVector3). From the release notes:
• A compiler crash when calling C or Objective-C functions that take
SIMD float3 parameters has been fixed. (21294916)
回答11:
Ok, in my case it was because I had an enum nested in a generic class. Now, the strange thing, is that when I isolated the problem (into the BaseDao2), the compiler told me the right error, but in my real BaseDao implementation (which has more stuff), it throw "trap 6".
Type 'DaoError2' nested in generic type 'BaseDao2' is not allowed
When I had this:
class BaseDao2<T>: InjectRestSession{
enum DaoError2: ErrorType{
case FAILED_RESPONSE(String)
case INVALID_RESULT(String)
case FAIL_TO_LIST, FAIL_TO_GET
}
func get() -> T?{
return nil
}
}
Anyway, in my case, I move the DaoError out of the BaseDao and everything compiled. Anyway, my feeling is that "trap 6" is what something cannot compile and the compiler got confused. Starting from a simple case, and adding back what you think might be causing the problem can be helpul to identify the problem by getting the right compile error. In other word, you have to be gentle with the swift compiler.
回答12:
This is what caused the error for me.
Before:
for (key,value) in hash{
count += value.count
}
After:
for (_,value) in hash{
count += value.count
}
It didn't like it that key was never being used anywhere. I am not sure why it should cause the build to fail though.
回答13:
I managed to get my project to build by setting the optimisation level to 'None' under the 'Swift Compiler - Code Generation' menu in the target (not the project) settings. See the screenshot below...
This shouldn't be a permanent solution because it more than doubles the size of the ipa. It should be possible to switch optimisation back on when Xcode 7 comes out of beta.
回答14:
In my case i had @objc protocol with optional methods and when i called its methods also in swift class i got that error, after removing the optional keyword from functions in the protocol the error was gone.
before (with error):
@objc protocol SomeDelegate:NSObjectProtocol{
optional func someDelegateMethod()
}
class MySwiftClass{
func notifyMyDelegate(){
mydelegate?.someDelegateMethod?() //this line caused the error
}
}
after:
@objc protocol SomeDelegate:NSObjectProtocol{
func someDelegateMethod()
}
class MySwiftClass{
func notifyMyDelegate(){
mydelegate?.someDelegateMethod()
}
}
回答15:
for me.. I modified the content of a @objc function like so:
before:
@objc func didConnectWithSession() {
context!.stream.disconnectAfterSending()
}
after:
@objc func didConnectWithSession() {
//context!.stream.disconnectAfterSending()
}
This caused the error. I resolved by removing the entire function.
回答16:
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 {
...
}
}
回答17:
In my case I had a private struct Constants
declared in both class A
and extension A
.
Probably it should be giving an error but it didn't.
回答18:
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.
回答19:
I resolved this problem with these steps:
ran 'pod deintegrate'
Makesure podfile like this:
platform :ios, '8.0'
use_frameworks!
ran 'pod install'
回答20:
In my case, renames several parameters of init methods which is a protocol fails the compilation. I solve it by do it one by one, compiles again after each change.
回答21:
I was having this same problem and I found the problem was that I had changed the build system to "New Build System" after reading an article on "how to speed up your builds" (This is the article btw here)
So to go back to the standard build system:
- To enable the new build system, go to File → Project Settings (or Workspace Settings).
- Change Build System to Standard Build System.
Hope it helps someone and don't waste hours trying to find out why this was not working!
回答22:
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
回答23:
In Xcode 9.3, I have re-installed the specific pod, in which warnings appeared and cleared derived data again n again. It worked for me :)
回答24:
I got this error when attempting to run tests.
To solve it, I put this script into Terminal:
rm -rf ~/Library/Developer/Xcode/DerivedData
Deleting derived data resolved the issue
回答25:
I fixed it by going to Xcode -> Preferences -> Locations -> Set Relative
option to Derived Data.
回答26:
For me it was MD5.swift issue
What you have to do is search in your project for file name "MD5.swift" even in the pods
and replace all the content with this file here
https://github.com/onmyway133/SwiftHash/blob/master/Sources/MD5.swift
回答27:
This issue is still present in Xcode 10.2.1. After cleaning the build folder, the error went away.
回答28:
I was able to resolve it by making a change to the bridging header. In my case adding a line break was sufficient. Very strange bug.
回答29:
For me below statement cause an error.
let dict = mainDict as [String:String]
Fix the issue by force unwraps
let dict = mainDict as! [String:String]
There is nothing related to the compiler. It is just type casting issue, Apple should give proper description instead of the giving compiler error
回答30:
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.