Swift compiler segmentation fault when building

后端 未结 30 2272
南旧
南旧 2020-11-29 03:59

Adding a (convenient) computed height property to UIView in my UIViewExtension.swift file is causing the Swift compiler to segfault...

相关标签:
30条回答
  • 2020-11-29 04:45

    I catch some exception today

    class func createByAny(instance: Any?) -> ApiCollectionResponse { ... }
    

    and this solved it:

    class func createByAny(instance: Any) -> ApiCollectionResponse { ... }
    

    Because "Any" type is any type event "nil", "AnyObject", optional, ... :) It is cannot be optional, it is already optional.

    typealias Any = protocol<>
    
    0 讨论(0)
  • 2020-11-29 04:46

    I had the same problem in an extension. My extension had two convenience initializers:

    convenience init(context: NSManagedObjectContext) {
        let entityDescription = NSEntityDescription.entityForName("PropertyEntity", inManagedObjectContext: context)!
        self.init(entity: entityDescription, insertIntoManagedObjectContext: context)
    }
    
    convenience init(dictionary: NSDictionary, context: NSManagedObjectContext) {
        self.init(context: context)
        property1 = (dictionary["key"] as? String) ?? ""
        // More properties...
    }
    

    To get rid of the error I added an instance method map(dictionary: NSDictionary) and the segmentation fault error disappeared.

    convenience init(dictionary: NSDictionary, context: NSManagedObjectContext) {
        self.init(context: context)
        map(dictionary)
    }
    
    0 讨论(0)
  • 2020-11-29 04:47

    Ran into this error because of an extraneous generic type on an operator function, e.g.

    func ==<T>(lhs: Foo, rhs: Foo) -> Bool {
      return lhs.bar == rhs.bar
    }
    

    In my case, removing <T> resolved the issue.

    0 讨论(0)
  • 2020-11-29 04:49

    Im my case, this happened when I did incorrect static initialization in a protocol. I found a way to get around, but a compiler should never produce a segmentation fault while building.

    There are three files involved. A protocol NamedSegues.swift, a custom TableViewController that among other things implements the protocol which contains a callback, a custom TableViewCell that holds reference to this protocol to call the callback.

    //file1
    import Foundation
    protocol NamedSegues {
        func  executeSegueWithId(id: String) -> Void
        static func getDefault() -> NamedSegues  // This was required because of init requirement in CustomCellView
    }
    
    
    //file2
    class CustomController: UITableViewController, NamedSegues {
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellID", forIndexPath: indexPath ) as! CustomCellView
    
            // Configure the cell...
           //App logic...
    
            cell.parent = self
    }
    
     //Mark: NamedSegues
         func executeSegueWithId(id: String) ->() {
            NSLog("Received callback to execute segue: \(id)")
            //
        }
    
        static func getDefault() -> NamedSegues { // I think this must be where it threw up.
            return self as! NamedSegues
        }
    
    }
    
    
    //file3
    
    import UIKit
    
    class CustomCellView: UITableViewCell {
    
        var id: String = "NoName"
        var parent: NamedSegues = NamedSegues.getDefault() // This is where it was needed.
    
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
            NSLog("Touched id: \(id)")
    
    
                 parent.executeSegueWithId(id) // This is where parent was used.
         }
    }
    

    I got around it by using ?

    In the protocol file, file1: delete the declaration of getDefault() In the CustomController file2: delete the implementation of getDefault. In the CustomCellView, file3:

        var parent: NamedSegues?
        ...
        parent?.executeSegueWithId(id)
    

    The compiler should have caught this and given some error message instead of throwing a segmentation fault during build!

    0 讨论(0)
  • 2020-11-29 04:49

    in my case, I tried to add a function parameter after a variadic parameter.

    Reversing parameter sequence and making the variadic parameter the last parameter in the parameter list fixed it.

    0 讨论(0)
  • 2020-11-29 04:50

    This error happened to me when I tried to override weak variable from parent class.

    In base class:

    weak var stripeViewDelegate : StripeViewDelegate? = nil    
    

    Derived class:

    override weak var stripeViewDelegate : StripeViewDelegate? = nil {
        didSet {
            self.stripeView.delegate = stripeViewDelegate
    
        }
    

    The error disappeared when I removed =nil from derived class.

    0 讨论(0)
提交回复
热议问题