我想問一下如何解決我的問題。我只是簡單地將一些「門戶」附加到依賴國家。每個「門戶」不止一次,我不想追加。避免在追加對象的同時追加值
我有下面的類定義:
class cls_main{
var countries:[cls_country]!
init() {
countries = [cls_country]()
}
// "add Country"
func addCountry(iCountry:cls_country) {
countries.append(iCountry)
}
}
class cls_country{
var countryName:String!
var portals:[cls_portal]!
init() {
portals = [cls_portal]()
}
// "add Portal"
func addPortal(portName:String) {
var tmpPortal = cls_portal()
tmpPortal.portalName = portName
println("-->Input Portal: \(tmpPortal.portalName)")
if portals.count == 0 {
portals.append(tmpPortal)
} else {
for port in portals {
if port.portalName == portName {
println("SAME INPUT, DONT SAVE")
} else {
portals.append(tmpPortal)
}
}
}
}
func arrayCount(){
println("Portals : \(portals.count)")
}
}
class cls_portal{
var portalName:String!
}
所以我將其稱之爲:
var MAIN = cls_main()
var country = cls_country()
country.countryName = "USA"
country.addPortal("Dance")
country.addPortal("Dance") // Should not be appended...
country.addPortal("Hike")
country.addPortal("Swim")
country.addPortal("Play")
MAIN.addCountry(country)
country = cls_country()
添加值後林遍歷值,並打印出來。其結果將是這樣的:
循環:
for country in MAIN.countries {
println("COUNTRY: \(country.countryName)")
if country.countryName == "USA" {
for portal in country.portals {
println(" -> PORTAL : \(portal.portalName)")
}
country.arrayCount()
}
}
結果:
-->Input Portal: Dance
-->Input Portal: Dance
SAME INPUT, DONT SAVE
-->Input Portal: Hike
-->Input Portal: Swim
-->Input Portal: Play
COUNTRY: USA
-> PORTAL : Dance
-> PORTAL : Hike
-> PORTAL : Swim
-> PORTAL : Swim
-> PORTAL : Play
-> PORTAL : Play
-> PORTAL : Play
-> PORTAL : Play
Portals : 8
那麼,爲什麼每次和各門戶網站將在年底被乘以?非常感謝你。