1

I have created a swift extension for NSAttributedString.

import Foundation

extension NSAttributedString {
    public func trimming(_ charSet: NSCharacterSet) -> NSAttributedString {
        let modifiedString = NSMutableAttributedString(attributedString: self)
        modifiedString.trimCharactersInSet(charSet: charSet)
        return NSAttributedString(attributedString: modifiedString)
    }
}

extension NSMutableAttributedString {
    public func trimCharactersInSet(charSet: NSCharacterSet) {
        var range = (string as NSString).rangeOfCharacter(from: charSet as CharacterSet)
        // Trim leading characters from character set.
        while range.length != 0 && range.location == 0 {
            replaceCharacters(in: range, with: "")
            range = (string as NSString).rangeOfCharacter(from: charSet as CharacterSet)
        }

        // Trim trailing characters from character set.
        range = (string as NSString).rangeOfCharacter(from: charSet as CharacterSet, options: .backwards)
        while range.length != 0 && NSMaxRange(range) == length {
            replaceCharacters(in: range, with: "")
            range = (string as NSString).rangeOfCharacter(from: charSet as CharacterSet, options: .backwards)
        }
    }
}

I want to use it from both objective c and swift. Using it from swift is working fine but some how from objective c its unable to find that method.

In the objective C i am also importing #import "myproject-Swift.h" and using it like this

[dataObject.htmlText trimmimg:NSCharacterSet.whitespaceAndNewlineCharacterSet];

htmlText is an NSAttributedString but when i am using it it gives me the following error.

 No visible @interface for 'NSAttributedString' declares the selector 'trimmimg:'

I am using Xcode 9. Can some one guide how this could be used in Objective C

Madu
  • 4,849
  • 9
  • 44
  • 78

0 Answers0