Swift startIndex from string -
i'm having weird bug in code. i'm building ios app in swift. code contains following function:
func generateattrstring(path: string?) { { let content = try string(contentsoffile: path!, encoding: nsutf8stringencoding) let array = content.componentsseparatedbystring("\n¶") let attrstyle = nsmutableparagraphstyle() attrstyle.linespacing = 8 attrstyle.paragraphspacing = 12 element in array { var string = element var attributes = [ nsparagraphstyleattributename: attrstyle, nsfontattributename: uifont(name: "myriadpro-light", size: 17.0)! ] var attrstring = nsmutableattributedstring(string: string, attributes: attributes) if string[string.startindex] == "•" { string.removeatindex(string.startindex) attributes[nsfontattributename] = uifont(name: "myriadpro-regular", size: 17.0)! let newattrstyle = attrstyle.mutablecopy() as! nsmutableparagraphstyle newattrstyle.paragraphspacing = 3 newattrstyle.linespacing = 0 attributes[nsparagraphstyleattributename] = newattrstyle attrstring = nsmutableattributedstring(string: string, attributes: attributes) } contentarray.append((false, attrstring)) } } catch _ { } }
at specific part of:
if string[string.startindex] == "•" {
it giving me error, tried adding breakpoint before, , tried printing "string", returned me string. after tried string[string.startindex] == "•", returned true. if run code , try opening page code runs following error:
fatal error: can't form character empty string
any idea on how solve this? string don't seems empty...
thanks!
if have empty string you'll see error. if test first make sure string isn't empty should okay.
if !string.isempty && string[string.startindex] == "•" {
Comments
Post a Comment