加入一组自定义对象

时间:2021-10-16 19:40:29

I've created a custom object which contain id, name and shortname. I would like to only retrieve the id's and do a ",".join() so that it will be a string like for instance "1, 2"

我创建了一个包含id,name和shortname的自定义对象。我想只检索id并执行“,”。join(),这样它就像一个字符串,例如“1,2”

So how can I convert an array like var recentArray = Array<News>() to an string with only the id's seperated by comma?

那么如何将var recentArray = Array ()这样的数组转换为只用逗号分隔id的字符串?

Custom Class

class Team: NSObject{
    var id: Int!
    var name: NSString!
    var shortname: NSString!


    init(id: Int, name:NSString, shortname: NSString) {
        self.id = id
        self.name = name
        self.shortname = shortname
    }


    required convenience init(coder aDecoder: NSCoder) {
        let id = aDecoder.decodeIntegerForKey("id")
        let name = aDecoder.decodeObjectForKey("name") as! String
        let shortname = aDecoder.decodeObjectForKey("shortname") as! String
        self.init(id: id, name: name, shortname: shortname)
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeInteger(id, forKey: "id")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(shortname, forKey: "shortname")
    }    
}

3 个解决方案

#1


you have tu use map function.

你有使用地图功能。

var t1 = Team(id: 1, name: "Adria", shortname: "Ad")
var t2 = Team(id: 2, name: "Roger", shortname: "Ro")
var t3 = Team(id: 3, name: "Raquel", shortname: "Ra")

var array: [Team] = [t1, t2, t3];

var arrayMap: Array = array.map(){ toString($0.id) }
var joinedString: String  = ",".join(arrayMap)

println(joinedString) // 1,2,3

#2


map the objects to an array of strings, and then join that:

将对象映射到字符串数组,然后加入:

", ".join(recentArray.map { toString($0.id) })

#3


You can map the array to get just id's from the Team array and reduce the array to string:

您可以映射数组以从Team数组中获取id,并将数组减少为字符串:

let a = Team(id: 1, name: "Greg", shortname: "G") 
let b = Team(id: 2, name: "John", shortname: "J") 
let c = Team(id: 3, name: "Jessie", shortname: "Je") 
let d = Team(id: 4, name: "Ann", shortname: "A")

let array = [a,b,c,d]

let result = array.map({$0.id}).reduce("", combine: {result, id in return result == "" ? "\(id)" : "\(result),\(id)"})

#1


you have tu use map function.

你有使用地图功能。

var t1 = Team(id: 1, name: "Adria", shortname: "Ad")
var t2 = Team(id: 2, name: "Roger", shortname: "Ro")
var t3 = Team(id: 3, name: "Raquel", shortname: "Ra")

var array: [Team] = [t1, t2, t3];

var arrayMap: Array = array.map(){ toString($0.id) }
var joinedString: String  = ",".join(arrayMap)

println(joinedString) // 1,2,3

#2


map the objects to an array of strings, and then join that:

将对象映射到字符串数组,然后加入:

", ".join(recentArray.map { toString($0.id) })

#3


You can map the array to get just id's from the Team array and reduce the array to string:

您可以映射数组以从Team数组中获取id,并将数组减少为字符串:

let a = Team(id: 1, name: "Greg", shortname: "G") 
let b = Team(id: 2, name: "John", shortname: "J") 
let c = Team(id: 3, name: "Jessie", shortname: "Je") 
let d = Team(id: 4, name: "Ann", shortname: "A")

let array = [a,b,c,d]

let result = array.map({$0.id}).reduce("", combine: {result, id in return result == "" ? "\(id)" : "\(result),\(id)"})