Mohammed Aboelwafa Mohammed Aboelwafa - 3 months ago 22
Swift Question

Accessing NSMutableArray elements in SWIFT

I am facing a problem with NSMutableArray in swift , I am developing an application using indoor.rs iOS idk to build an indoor navigation application , I want to show all zones on my map and save its names and points. this is my code :

var allZones = [AnyObject]()
for floor in self._currentBuilding!.floors.allValues as! [IDSFloor] {
allZones.append(floor.zones as [AnyObject])
}

if let zone = allZones[index] as? IDSZone, let points = zone.points as? [IDSZonePoint] {
points.flatMap { (point) in
print(point.x)
print(point.y)
}

}


and the result is :

(
"<IDSZone: 0x166ede70>",
"<IDSZone: 0x166edf40>",
"<IDSZone: 0x166edfe0>",
"<IDSZone: 0x166ee070>",
"<IDSZone: 0x166ee0c0>",
"<IDSZone: 0x166ee130>",
"<IDSZone: 0x166ee180>",
"<IDSZone: 0x166ee1c0>",
"<IDSZone: 0x166ee210>"
)


How can i retrieve all zones data , please . This is link for documentation : https://indoors.readme.io/docs/zones-1

Thanks.

Answer

So, first of all I think you can declare your array:

let allZones = [IDSZone] or let allZones = [AnyObject]

(let me know if it creates some errors)

then:

allZones.appendContentsOf(floor.zones as [AnyObject]) // Swift 2
allZones.append(contentsOf: floor.zones as [AnyObject]) // Swift 3

and then you can access to array element like this:

allZones.first as? IDSZone
allZones[<your_index>] as? IDSZone
...

to get the points:

if let zone = allZones[index] as? IDSZone, let points = zone.points as? [IDSZonePoint] {
points.flatMap { (point) in
    print(point.x)
    print(point.y)
}

for the name you can use the id if you want.

Comments