12

i have a problem with a code that throw the next message:

error: cannot convert value of type 'String' to type 'NSString' in coercion
            return (self as NSString).substringWithRange(range)

I could resolve it before but not with a self calling, so here is the code:

let range = expression.rangeOfFirstMatchInString(self, options: [], range: NSMakeRange(0, self.utf16.count))
    if range.location != NSNotFound {
        return (self as NSString).substringWithRange(range)
    }
    return nil
2
  • 1
    Try return NSString(string: self)... May 18, 2016 at 7:53
  • yes, it worked, thanks :) May 18, 2016 at 8:03

1 Answer 1

20

The swift compiler in Ubuntu won't auto recognize that NSString has a constructor that gets String as argument. (at build time the compiler interprets it)

Instead do the work by your self by writing

NSString(string: self)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.