简体   繁体   中英

cannot convert value of type 'String' to type 'NSString' in coercion when i use self swift2 with ubuntu 16.04

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM