1

let me start with the code:

    var lat1:CLLocationDegrees = 50.102760 

    var lat2:CLLocationDegrees = -26.135170

    .
    .
    .

    var lat191:CLLocationDegrees = 60.289139

    var LocCount = 191

    for var iLat = 3; iLat == LocCount; ++iLat{
            AirportLat.append("lat" + iLat)    
     }

What i try is go to count up the name of the var that should be appended to my array is there a way to do this?

1
  • You probably want an array of CLLocationCoordinate2D (latitude and longitude) structs instead of just CLLocationDegrees. Even better would be to create an Airport class with a CLLocationCoordinate2D as one of the properties (as well as the airport name, airport code, etc). Commented Sep 10, 2014 at 11:14

2 Answers 2

2

Well, that's a difficult way to do it.

How about:

var latArray = <CLLocationDegrees>() 
latArray += [50.102760] 
latArray += [-26.135170]
.
.
.
latArray += [60.289139]

println("LocCount check = \(latArray.count)")

A better still way to do it would be to put the latitudes into a file (so you can change them, add to them, etc. without recompiling your code) and read them in, but I'll leave that as an exercise...

...and if you want to use the other answer's dictionary idea, you can add

for i in 0 ..< latArray.count {
    dict["lat\(i+1)"] = latArray[i]
}
Sign up to request clarification or add additional context in comments.

Comments

2

First off its not good practice to define 191 variables. Use Dictionary instead where keys are: lat1, lat2 .... lat191.

After that you can loop over Dictionary values and manipulate with them.

But if you still want to aggregate all variables to Array you can use class_copyIvarList to fetch all variables start with lat and write as:

class Myclass : NSObject{

    var lat1:CLLocationDegrees = 50.102760
    var lat2:CLLocationDegrees = -26.135170
    var lat3:CLLocationDegrees = 60.289139

    var AirportLat:Array<CLLocationDegrees> = []

    func fetchValues() -> Array<CLLocationDegrees>{
        var aClass : AnyClass? = self.dynamicType
        var propertiesCount : CUnsignedInt = 0
        let propertiesInAClass : UnsafeMutablePointer<Ivar> = class_copyIvarList(aClass, &propertiesCount)

        for var i = 0; i < Int(propertiesCount); i++ {
            var propName:String = NSString(CString: ivar_getName(propertiesInAClass[Int(i)]), encoding: NSUTF8StringEncoding)

            var propValue : AnyObject! = self.valueForKey(propName)

            if propName.hasPrefix("lat") {
               AirportLat.append(propValue as CLLocationDegrees)
            }

        }//for

        return AirportLat
    }       
}

var cc = Myclass()
cc.fetchValues()

Output:

[50.10276, -26.13517, 60.289139]

Comment: the class must inherit NSObject

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.