0

I'm quite unused to code with swift, it's very new for me (more used to code on Java or c#..)

I'm trying to do something very "easy" on other langages but I don't succeed to filter as I want a string, this is the code:

let id = self.Id.text
let pass = self.Pass.text
let documentDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as! NSURL
let fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("file.txt")
let text = "Identifiant: " + id + "\n" + "Password: " + pass
text.writeToURL(fileDestinationUrl, atomically: true, encoding: NSUTF8StringEncoding, error: nil)

This first portion of my code write on a text file datas, this is what the text file returns:

Identifiant: testId
Password: testPass

I'm tying to retrieve what id get and what pass get (in this case, testId and testPass..):

let startIndex = advance(text.startIndex, 14)
let endIndex = advance(startIndex, "to the \n character?")
//From the : to the \n, and then from the other : to the end, like shown after with Java code
let range = startIndex..<endIndex
var idFinal = text[range]

This is where I'm blocked. I have found the way to filter from a certain character (here the 14th), but I don't find the way to stop at a defined character with an unknown position. If anyone knows what am I supposed to do instead of "to the \n character"

With Java, I have done like this:

String ligne;
while((ligne = br1.readLine()) != null){
    if(ligne.startsWith("Identifiant: ")){
        System.out.println(ligne);
        id = ligne.substring(ligne.lastIndexOf(':')+2);
        }
    if(ligne.startsWith("Pass: ")){
        System.out.println(ligne);
        pass = ligne.substring(ligne.lastIndexOf(':')+2);
        }
    }

Is this possible with swift? I hope I have been enough precise, I can add code or explanations if needed.

Thanks in advance.

Matt
  • 17,290
  • 7
  • 57
  • 71
fselva
  • 457
  • 1
  • 7
  • 18

0 Answers0