I'm trying to figure out how to loop through this usernames array, but am not sure the syntax. I am using swift in xcode
I currently have:
// Read text file
if let filepath = NSBundle.mainBundle().pathForResource("barbers", ofType: "txt")
{
do
{
//let contents = try NSString(contentsOfFile: filepath, usedEncoding: nil) as String;
//print(contents);
let text = try NSString(contentsOfFile: filepath, usedEncoding: nil) as String;
// Create character array & parse
let usernames = text.characters
.split { $0 == "\n" }
.map { String($0) }
.map { String($0.characters.split(" ")[0]) }
// Print Users
print(usernames);
// Test
}
catch
{
// Catch error?
}
}
else
{
// Print error to console log
print("Error: Could not find text file - something went wrong");
}
My question is: How can I loop through my usernames array?
I just dont know the swift syntax
I'm looking for something like
for(int i =0; i < usernames.size(); i ++ )
{
if(usernames[i] == USER)
{
b = true;
break;
}
b = false;
}
UPDATES:
Ok so I've figured out how to loop through but now I'm having troubles
I made a global variable
var abc = "";
I then did
let abc = usernames;
now when I try to do this
// Test
for i in abc.characters
{
print("abc array contents: " + i);
if(i == theUser)
{
print("Barber");
barb = true;
break;
}
print("USER " + theUser);
barb = false;
print("\n");
}
I get the error
Binary operator '+' cannot be applied to operands of type 'String' and 'Character'
and
Binary operator '==' cannot be applied to operands of type 'Character' and 'String'
if usernames.contains(USER) { ... }