简体   繁体   中英

Appending new values to a generic array

I am trying to append new values to a generic array. The problem is that the last value appended to the array appears as the content for all the array objects. I guess I am doing a terrible unforced error... Could anyone point that out ?

    // Playground - noun: a place where people can play


import Foundation
import CoreLocation

class UrgentCenterDetails{

    var latitude:CLLocationDegrees
    var longitude:CLLocationDegrees
    var title:String
    var subtitle:String
    var isiBeaconEnabled:Bool

    init(){
        title = "Default"
        subtitle = "Default entry"
        latitude = 0.0
        longitude = 0.0
        isiBeaconEnabled = false

    }

    func setCenterDetails(latitude:CLLocationDegrees, longitude:CLLocationDegrees, title:String, subtitle:String, isiBeaconEnabled: Bool){
        self.title = title
        self.subtitle = subtitle
        self.latitude = latitude
        self.longitude = longitude
        self.isiBeaconEnabled = isiBeaconEnabled
    }

}

var urgentCenters:Array<UrgentCenterDetails> = []

var center:UrgentCenterDetails = UrgentCenterDetails()
var title:String
var subtitle:String
var latitude:CLLocationDegrees
var longitude:CLLocationDegrees
var iBeacon:Bool

title = "Hospital of the University of Pennsylvania"
subtitle = "UrgentCare Center 1";
latitude =  39.9532293
longitude = -75.194119
iBeacon = false
center.setCenterDetails(latitude, longitude:longitude, title:title, subtitle:subtitle, isiBeaconEnabled: iBeacon)
urgentCenters.append(center)
println("\(urgentCenters[0].title)")

title = "Drexel Hospital"
subtitle = "UrgentCare Center 2"
latitude =  39.95661270
longitude =  -75.18994409
iBeacon = false
center.setCenterDetails(latitude, longitude:longitude, title:title, subtitle:subtitle, isiBeaconEnabled: iBeacon)
urgentCenters.append(center)
println("\(urgentCenters[0].title)")
println("\(urgentCenters[1].title)")

You're not creating second instance of center object. Instead you're replacing its properties (and also updating its first copy that is already in your array) and then adding it again.

You need to add something like

center = UrgentCenterDetails()

after you added it once to the array

You're adding the same object. If you later change its values, it's still the same object. Make a new center = UrgentCenterDetails() for your second case.

It's kind of like this: your friend George wants to submit his house to the Nicest Painted House Pageant, and asks you for advice. You tell him his house would look great in green, so he goes and paints it green, and calls judges to come see it. Then you change your mind and decide pink would be better, so he repaints his house and calls the judges again. Judges, being busy people, can't come at once, so they come tomorrow, but don't notice the same address, and come see his house twice . Unsurprisingly, they see two pink houses , not a green and a pink one. If you want the judges to see two houses and not the same one twice, show them two different houses.

Same thing with your array: there's two references to the same object, that ends up being about Drexel when you come around to looking at the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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