Failed to execute command due to signal: Segmentation fault: 11 when emitting IR SIL function

I want to add closure properties to an extension UITextView

, so I define a closure using typealias:

typealias TextViewHeightDidChangedClosure = (_ currentTextViewHeight:CGFloat)->Void

extension UITextView{

  func setTextViewHeightDidChanged(textViewHeightDidChanged:TextViewHeightDidChangedBlock){
    objc_setAssociatedObject(self, &TextViewHeightDidChangedBlockKey, textViewHeightDidChanged, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
  }

  func textViewHeightDidChanged()->TextViewHeightDidChangedBlock?{
   let textChanged : ((CGFloat)->Void) = objc_getAssociatedObject(self, &TextViewHeightDidChangedBlockKey) as! TextViewHeightDidChangedBlock
    return textChanged
  }

}

      

But this tells me the error:

Command failed due to signal: Segmentation fault: 11.

Here is an image of the error

my code

Can anyone tell me why and give me a deep meaningful explanation, thank you very much!

+3


source to share


4 answers


You can also get this error if you declare a property Bool!

in a class and try to make a ternary condition with that property:

var isSomething: Bool!

func myFunc() {
    let value = isSomething ? "something" : "not"
}

      



Just add! on your property

var isSomething: Bool!

func myFunc() {
    let value = isSomething! ? "something" : "not"
}

      

+10


source


Generally, SegFault 11 crash is a compiler bug and you'd better file a bug report .

And most of these errors can be circumvented by fixing the code.

The worst thing about the code is that normal closures in Swift ( @convention(swift)

- usually omitted) cannot be passed to Any

that represents id

from Objective-C. Use @convention(block)

which is listed as id

-compatible in the Swift book ( this part ):

typealias TextViewHeightDidChangedBlock = @convention(block) (_ currentTextViewHeight:CGFloat)->Void

      

You may have tried this, but in this case, just putting @convention(block)

it down won't solve the problem.



You seem to need another trick to get your code to work by explicitly clicking on AnyObject

:

extension UITextView{

    func setTextViewHeightDidChanged(textViewHeightDidChanged: @escaping TextViewHeightDidChangedBlock){
        objc_setAssociatedObject(self, &TextViewHeightDidChangedBlockKey, textViewHeightDidChanged as! AnyObject, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
    }

    func textViewHeightDidChanged()->TextViewHeightDidChangedBlock?{
        let textChanged : ((CGFloat)->Void) = objc_getAssociatedObject(self, &TextViewHeightDidChangedBlockKey) as! TextViewHeightDidChangedBlock
        return textChanged
    }

}

      

So this seems like a disadvantage id-as-Any

. Any

that represents id

must accept an id

-compatible closure.

I'm not sure if Apple will fix this bug soon, but as far as I checked, my code above worked as expected anyway.

+5


source


For me it was because I was declaring the protocols for the two extensions in a generic class and implementing them in the class declaration. I am using xCode 9 swift 4

+1


source


I faced a similar issue in Xcode 9.3, I opened the given swift class as indicated in the error image (I had different), went to the method (error mention), there was code like below

class func makeViewCircular(view: AnyObject) {
        let viewCircle = view
        viewCircle.layer.cornerRadius = view.frame.size.width/2
        viewCircle.layer.borderColor = UIColor.clear.cgColor
        viewCircle.layer.borderWidth = 1.0
    }

      

So, I am editing the code as shown below. Fixed problem. The problem was the AnyObject type. I am casting AnyObject to UIView.

  class func makeViewCircular(view: AnyObject) {
        if let viewCircle = view as? UIView{
            viewCircle.layer.cornerRadius = view.frame.size.width/2
            viewCircle.layer.borderColor = UIColor.clear.cgColor
            viewCircle.layer.borderWidth = 1.0
        }
    }

      

0


source







All Articles