简体   繁体   English

快速遍历数组时语法有麻烦

[英]Having trouble with syntax in swift looping through an array

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 我在xcode中使用swift

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' 二进制运算符'=='不能应用于'Character'和'String'类型的操作数

for username in usernames
{
    if(username == USER)
    {
        b = true;
        break;
    }
    b = false;
}

or if you need access to current item's index: 或如果您需要访问当前项目的索引:

for (index, username) in usernames.enumerate()
{
    // same body as above
}

All of these above work, but my favorite Swifty for loop is 以上所有这些工作,但我最喜欢的Swifty for循环是

  for index in 0..< usernames.count { 
     //your code here
     //to get an object at a specific index you call usernames[index]
  }

In response to your updated question I believe you need to cast your character as a string. 回答您的最新问题,我相信您需要将字符转换为字符串。 Something like 就像是

  let myCharacterAsAString = String(i)
  print("abc array contents: " + myCharacterAsAString);
  if(myCharacterAsAString == theUser)
  {
    print("Barber");
    barb = true;
    break;
  }
  print("USER " + theUser);
  barb = false;
  print("\n");
}

I believe latest versions of Swift have done away with the C-style for-loop. 我相信Swift的最新版本已经取消了C样式的for循环。 This might not be super helpful but the way to iterate in Swift would likely be with a for-in loop 这可能不是超级有帮助,但是在Swift中进行迭代的方法可能是使用for-in循环

for username in usernames
{
    if username == USER
    {
        b = true
        break
    }
    b = false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM