How to increment String in Swift -


i need save files in alphabetical order.
code saving files in numeric order

1.png
2.png
3.png
...

the problem when read files again read files described here


so thinking of changing code , save files not in numeric order in alphabetical order as:

a.png
b.png
c.png
...
z.png
aa.png
ab.png
...

but in swift it's difficult increment character type.
how can start from:

var s: string = "a" 

and increment s in way?

paste code in play ground , check result. n numbers supported means can enter high number such : 99999999999999 enjoy!

you can uncomment for loop code check code working fine or not don't forgot assign lesser value counter variable otherwise xcode freeze.

//: playground - noun: place people can play  import uikit  var filename:string var counter:double = 0 var alphabets:array = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] let totalalphabets:double = 26.0 let numfiles = 9999   func getcharacter(counter c:double) -> string {      var chars:string;     var divisionresult:int = int(c / totalalphabets)     let modresult:int = int(c % totalalphabets)      chars = getcharfromarr(index: modresult)      if(divisionresult != 0){          divisionresult -= 1;          if(divisionresult > alphabets.count-1){             chars = getcharacter(counter: double(divisionresult)) + chars         }else{             chars = getcharfromarr(index: divisionresult) + chars         }     }      return chars; }  func getcharfromarr(index i:int) -> string {     if(i < alphabets.count){         return alphabets[i];     }else{         print("wrong index")         return ""     } }  //for _ in 0...numfiles { //    filename = getcharacter(counter: counter) //    print(filename) //    counter += 1; //}  filename = getcharacter(counter: numfiles)+".png" 

Comments