Remove image (NSTextAttachMent) from NSMutableAttributedString
<p>I am having trouble completely removing an image and all the "meta data" from the NSAttachMent in the NSMutableAttributedString. To give a high level explanation of my scenario - a user can upload an image or text to a social media feed. I use UITextView for the input and display for both the text and image. So my thought is if the user wants to insert a message and an image (vice versa too), I use the following code below:</p>

<pre><code>func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let image = info[UIImagePickerControllerOriginalImage] as? UIImage

    imagePicker?.dismissViewControllerAnimated(true, completion: nil)

    let mutableAttrString = NSMutableAttributedString()

    let attachment = NSTextAttachment()

    var width : CGFloat?
    var height : CGFloat?

    let size = CGSizeMake(width!, height!)

    let imgToDisplay = scaleImageWithoutChangingAspect(image!, toSize: size)

    attachment.image = imgToDisplay

    mutableAttrString.appendAttributedString(format.prepAtrributedString(textToStore + "\n", f: UIFont(name: fontRegular, size: 20)!, Color: UIColor.blackColor()))

    let attString = NSAttributedString(attachment: attachment)

    mutableAttrString.appendAttributedString(attString)
    //add this attributed string to the current position.

    self.TextMessage.textStorage.insertAttributedString(mutableAttrString, atIndex: self.TextMessage.selectedRange.location)
    self.TextMessage.attributedText = mutableAttrString
    self.TextMessage.invalidateIntrinsicContentSize()
    self.TextMessage.updateConstraintsIfNeeded()
    self.TextMessage.layoutIfNeeded()
}
</code></pre>

<p>Here is the code I use to attempt to remove the image from the attributed text, however after the removal I see a unicode character <code>\u{ef}</code>:</p>

<pre><code>let s = self.TextMessage.text

self.TextMessage.resignFirstResponder()

print(s)


let mutAttrString = NSMutableAttributedString(attributedString: self.TextMessage.attributedText)

let range = NSMakeRange(0, mutAttrString.length)

mutAttrString.enumerateAttributesInRange(range, options: NSAttributedStringEnumerationOptions.Reverse) { (OBJS, Range, B) -&gt; Void in
    for s in OBJS {
        if s.0 == "NSAttachment" {
            mutAttrString.removeAttribute(s.0, range: Range)
        }
    }
}


mutAttrString.endEditing()

var insertMessageData = mutAttrString.string

let db = SwiftDatabase()

var ImgData : NSData?

if let i = selectedImage {
    ImgData =   UIImageJPEGRepresentation(i, 0.5)
}

///Insert text and upload image to database.
let tup =  db.insertShowMessage(insertMessageData, data: ImgData,delegate: self)
key_id = tup.KeyId
query = tup.Query

hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
</code></pre>

<p>I also tried converting the text to ASCII, but I still see the "Meta data" NSTextAttachMent. Instead of an unicode character, I see a "?."</p>

<p>I would greatly appreciate any help. Thanks!</p>