Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command failed due to signal: Segmentation fault: 11 while emitting IR SIL function

I want to add closure properties in the extension of 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 it tells me an error that says:

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 an deep meaningful explanation, thank you very much!

like image 335
GoldWater Avatar asked Dec 05 '16 06:12

GoldWater


3 Answers

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

like image 83
Exception Avatar answered Nov 18 '22 06:11

Exception


Generally, crashing with SegFault 11 is a bug of compiler and you'd better send a bug report.

And most such bugs can be worked around with fixing your code properly.

The most significantly bad thing in your code is that usual closures in Swift (@convention(swift) -- usually omitted) cannot be passed to Any which represents id of Objective-C. Use @convention(block) which is stated 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) does not solve the issue.

It seems another trick is needed to make your code work, explicitly cast to 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 to be a flaw of id-as-Any. Any which represents id should accept id-compatible closure.

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

like image 24
OOPer Avatar answered Nov 18 '22 08:11

OOPer


You can also have this error if you declare a Bool! property in a class and you try to make a ternary condition with this property:

var isSomething: Bool!

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

Just add the ! on your property

var isSomething: Bool!

func myFunc() {
    let value = isSomething! ? "something" : "not"
}
like image 43
Beninho85 Avatar answered Nov 18 '22 07:11

Beninho85