swift - NSScanner scanString: intoString: not working in playground -
i'm trying make swift version of xml parser som weather data:
import cocoa import foundation let string2: nsstring? = "cdefg jajaja <time> bbb" let scanner = nsscanner(string: string2) scanner.string let searchstring:nsstring? = "<time" var result: nsstring? = nil //= autoreleasingunsafepointer<nsstring?> scanner.characterstobeskipped = nil scanner.scanstring(searchstring, intostring: nil) // not working (false) scanner.scanuptostring( ">", intostring: &result) // working result // result = "cdefg jajaja <time"
why "scanner.scanstring(searchstring, intostring: nil)" returning nil?
actually, problem has nothing swift (equivalent objective-c code same) – you're using nsscanner
wrong. docs scanstring:intostring:
return value
yes if string matches characters @ scan location, otherwise no.
basically, scanstring:intostring:
either finds searchstring
@ current position (in example, @ start), or returns false
. since string starts "cdefg"
, not "<time"
, method returns false
, not advance scanning position. if remove "cdefg jajaja "
string, scanstring:intostring:
call return true
, advance scanning position next character ">"
. alternatively, change scanstring:intostring:
call scanuptostring:intostring:
discard before , including "<time"
.
Comments
Post a Comment