i've string looks array shown below. how can transform string array shown below? i've tried this solution got error xcode(command failed due signal segmentation fault 11) , i've thought way hard compiler.
string:
var list = "<strong>up</strong>, <strong>up</strong>, <strong>down</strong>, <strong>up</strong>"
target array:
var array = [<strong>up</strong>, <strong>up</strong>, <strong>down</strong>, <strong>up</strong>]
and when tried let arr = list.characters.split {$0 == ","}
prints:
[swift.string.characterview(_core: swift._stringcore(_baseaddress: 0x0000000103152e40, _countandflags: 9223372036854775811, _owner: nil)), swift.string.characterview(_core: swift._stringcore(_baseaddress: 0x0000000103152e48, _countandflags: 9223372036854775812, _owner: nil)), swift.string.characterview(_core: swift._stringcore(_baseaddress: 0x0000000103152e52, _countandflags: 9223372036854775817, _owner: nil)), swift.string.characterview(_core: swift._stringcore(_baseaddress: 0x0000000103152e66, _countandflags: 9223372036854775816, _owner: nil)), swift.string.characterview(_core: swift._stringcore(_baseaddress: 0x0000000103152e78, _countandflags: 9223372036854775815, _owner: nil))]
with whitespace(let arr = list.characters.split {$0 == ", "}
) gives compiler error:
- command failed due signal segmentation fault 11
would work:
var list = "<strong>up</strong>, <strong>up</strong>, <strong>down</strong>, <strong>up</strong>" { //create reggae , replace "," following spaces comma let regex = try nsregularexpression(pattern: ", +", options: nsregularexpressionoptions.caseinsensitive) list = regex.stringbyreplacingmatchesinstring(list, options: nsmatchingoptions.withoutanchoringbounds, range: nsmakerange(0, list.characters.count), withtemplate: ",") var array = list.characters.split { $0 == ","}.map(string.init) } catch { //bad regex created }
edit: updated example remove componentsseparatedbystring native swift way. problem example resulting array did not contain strings seems internal class called characterview. mapping these strings produces desired output.
output:
[ "<strong>up</strong>", "<strong>up</strong>", "<strong>down</strong>", "<strong>up</strong>" ]
Comments
Post a Comment