Ios 如何在Swift中将某些数据解析为数组?

Ios 如何在Swift中将某些数据解析为数组?,ios,swift,Ios,Swift,我正在读iOS/Swift中的一个.txt文件。我从文本文件中获取所有信息。问题是我只想获取用户名并将其存储在数组中。现在,我认为它正在以字符串形式存储.txt文件中的所有数据。我怎样才能正确地解析它,使数组中只有用户名 这是我的密码 if let filepath = NSBundle.mainBundle().pathForResource("myfile", ofType: "txt") { do { let contents = try N

我正在读iOS/Swift中的一个.txt文件。我从文本文件中获取所有信息。问题是我只想获取用户名并将其存储在数组中。现在,我认为它正在以字符串形式存储.txt文件中的所有数据。我怎样才能正确地解析它,使数组中只有用户名

这是我的密码

    if let filepath = NSBundle.mainBundle().pathForResource("myfile", ofType: "txt") {
        do {
            let contents = try NSString(contentsOfFile: filepath, usedEncoding: nil) as String
            print(contents)
        } catch {
            // contents could not be loaded
        }
    } else {
        print("UNABLE TO FIND TEXT FILE");
    }
我的文本文件格式:

用户名LastName

约翰·史密斯


adfkh carrie underwood

这是一个使用高阶函数的解决方案。希望这对你有用

let text = "tj thomas jeffereson \nbf benjamin franklin\ngw george washington"


let usernames = text.characters
    .split { $0 == "\n" }
    .map { String($0) }
    .map { String($0.characters.split(" ")[0]) }

print(usernames)
输出:

["tj", "bf", "gw"]
循环通过结果:

for user in usernames {
   print(user)
}

编辑:使用.plist文件添加解决方案

一种方法是,可以将信息存储到.plist文件中

  • 按command+N添加一个名为UserInformation.plist的新.plist文件(您可以用任何喜欢的名称替换UserInformation),并按如下所示进行编辑
  • 然后可以将用户名存储到数组中

    if let path = NSBundle.mainBundle().pathForResource("UserInformation", ofType: "plist") {
        if let data = NSArray(contentsOfFile: path) {
            let userName = NSMutableArray.init(capacity: 1)
            for dict in data {
                userName.addObject(dict.valueForKey("User")!)
            }
            // Now user names are stored into a array
        }
    }
    
    if let path = NSBundle.mainBundle().pathForResource("UserInfo", ofType: "json") {
        if let data = NSData(contentsOfFile: path) {
            do {
                let jsonData = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers ) as! NSMutableArray
                let userName = NSMutableArray.init(capacity: 1);
                for  dict in jsonData {
                    userName.addObject(dict.valueForKey("User")!)
                }
                // Now user names are stored into a array
            } catch {
                // report error
            } 
        }
    }
    
  • 另一种方法是,您也可以将信息存储到本地.json文件中,尽管json通常用于客户端和服务器之间的通信

    在iOS5中,苹果提供了支持解析JSON的API

  • 使用您喜欢的任何文本编辑器创建包含以下内容的json文件,并将其保存为UserInfo.json(您可以使用您喜欢的任何名称替换UserInfo)

  • 将其添加到您的项目中

  • 现在可以将用户名存储到数组中

    if let path = NSBundle.mainBundle().pathForResource("UserInformation", ofType: "plist") {
        if let data = NSArray(contentsOfFile: path) {
            let userName = NSMutableArray.init(capacity: 1)
            for dict in data {
                userName.addObject(dict.valueForKey("User")!)
            }
            // Now user names are stored into a array
        }
    }
    
    if let path = NSBundle.mainBundle().pathForResource("UserInfo", ofType: "json") {
        if let data = NSData(contentsOfFile: path) {
            do {
                let jsonData = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers ) as! NSMutableArray
                let userName = NSMutableArray.init(capacity: 1);
                for  dict in jsonData {
                    userName.addObject(dict.valueForKey("User")!)
                }
                // Now user names are stored into a array
            } catch {
                // report error
            } 
        }
    }
    

  • 有关JSON的更多详细信息,您可以访问。

    如果这是您希望随应用程序提供的固定文件,那么将所有数据放入plist文件会更容易。然后您可以直接将plist文件加载到数组中。文本文件是什么样子的?除非您告诉我们文本的格式,否则我们可能无法帮助您。rmaddy关于使用plist的建议是好的。通过这种方式,您可以将文件直接读入本机iOS数据结构,如数组、字典、字符串、整数等。我将使用文本文件格式更新OP,对此表示抱歉。确定使用文本文件格式查看我更新的帖子。这正是我需要的。谢谢,我如何循环使用该数组(用户名)。类似于for(inti=0;i