将数组元素追加到String(Swift)

时间:2016-06-07 17:53:06

标签: arrays swift dictionary key

我正在尝试为25个人创建一个ID的随机数组,所以我认为这将是使用数组映射的最简单方法,但有没有办法将每个元素并将其附加到&#34 ;电子邮件"在词典键中。

class ClassRosterModel {

var randomArray = (1...25).map{_ in arc4random()}


var studentsRoster = [Dictionary<String, String>] ()
init () {

    studentsRoster.append(["name": "Kacz, Alex", "major" : "SE", "email" : ".join(randomArray[0])@email.edu", "currentTerm" : "Spring", "numberOfCredits" : "" ])
    studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : ".join(randomArray[0]@email.edu", "currentTerm" : "Fall", "numberOfCredits" : "" ])

        }

}

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望在字符串中放置一个值。您可以使用\()

示例:

let value = 101
let email = "myname\(value)@email.com"
print(email)

控制台结果将是:

myname101@email.com

只需使用randomArray而不是value。并将电子邮件保存在字典中。

studentsRoster.append(["name": "Kacz, Alex", "major" : "SE", "email" : ".join\(randomArray[0])@email.edu", "currentTerm" : "Spring", "numberOfCredits" : "" ])
studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : ".join\(randomArray[0])@email.edu", "currentTerm" : "Fall", "numberOfCredits" : "" ])