2016-08-15 42 views
1

我在swift中遇到了NSMutableArray的一個問題,我正在開發一個應用程序,使用indoor.rs iOS idk來構建一個室內導航應用程序,我想在我的地圖上顯示所有區域並保存它的名稱和點。這是我的代碼:在SWIFT中訪問NSMutableArray元素

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) 
     } 

    } 

,其結果是:

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

我怎樣才能檢索到所有區域的數據,請。這是鏈接文件:https://indoors.readme.io/docs/zones-1

謝謝。

回答

1

所以,首先我覺得你可以聲明數組:

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

(讓我知道,如果它產生了一些錯誤)

則:

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

然後你可以像這樣訪問數組元素:

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

來獲得積分:

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

的名稱,如果你願意,你可以使用id。

+0

這是同樣的結果仍然不能正確的格式 –

+0

@MohammedAboelwafa你有一些錯誤檢索數據? –

+0

不,我搜索了idk文件,我發現一個名爲IDZone的文件有一個屬性屬性(非原子,強大的)NSMutableArray * points;當我嘗試打印它的價值我覺得這:( 「」, 「」, 「」, 「」 ) –

0

{

for floor in _currentBuilding?.floors.allValues as! [IDSFloor] { 
     allZones.addObjectsFromArray(floor.zones as [AnyObject]) 
    } 
    let realObject = allZones as NSArray as! [IDSZone] 
    for x in 0..<realObject.count { 
     let realPoints = NSMutableArray() 
     realPoints.addObjectsFromArray(realObject[x].points as [AnyObject]) 

     let zonePoints = realPoints as NSArray as! [IDSZonePoint] 

}這是解決它