ios - Why is my regex in Swift not compiling? How do I give it a mutable string instead? -
ios - Why is my regex in Swift not compiling? How do I give it a mutable string instead? -
when utilize next code strip http:// , www. off origin of url, error.
var error: nserror? = nil allow regex = nsregularexpression(pattern: "^(http(s)?://)?(www(\\d)?\\.)?", options: nil, error: &error) var stringy = "http://www.google.com/" regex.replacematchesinstring(stringy, options: nil, range: nsmakerange(0, countelements(stringy)), withtemplate: "") the error being:
'nsstring' not subtype of 'nsmutablestring'
how prepare this? doing wrong?
you can create stringy nsmutablestring constructing nsmutablestring(string: ...). you'll have few other things code work though:
nil options. if don't want pass options, right value nsmatchingoptions.allzeros in xcode 6.1 gm, init method you're using nsregularexpression returns optional (nsregularexpression?), you'll need utilize optional chaining phone call regex?.replacematchesinstring. (this may not case in xcode 6.0.1; i'm not sure when alter made) since stringy nsmutablestring now, can't phone call countelements() on it. utilize nsstring's length property instead. with changes in place, code looks like:
var error: nserror? = nil allow regex = nsregularexpression(pattern: "^(http(s)?://)?(www(\\d)?\\.)?", options: nil, error: &error) var stringy = nsmutablestring(string: "http://www.google.com") regex?.replacematchesinstring(stringy, options: nsmatchingoptions.allzeros, range: nsmakerange(0, stringy.length), withtemplate: "") then calling println(stringy) after it's executed outputs:
google.com
ios regex swift nsregularexpression
Comments
Post a Comment