0

In Core Data I have made 2 entities -- a) WeatherResponse which has 'location' as an attribute and b) Forecast which has 'Day', 'Temperature' and 'Condition' as the attributes. Below is the 'one to many' relationship.

enter image description here

The classes are below:

class CDWeatherResp: NSManagedObject {
@NSManaged var cdLocation: String?
@NSManaged var locForecast: NSOrderedSet?
}

class CDForecast: NSManagedObject {
@NSManaged var day: String?
@NSManaged var temperature: NSNumber?
@NSManaged var conditions: String?
@NSManaged var forecastLocation: NSManagedObject?
}

I have managed to save some data as NSOrderedSet in: CDWeatherResp.locForecast relationship. (This is working fine. Each index contains 1 forecast related info (i.e.: day, temperature & conditions)

Question: How do I access the attributes of the "CDForecast" class so that I can do something like 'CDWeatherResp.locForecast.day'

I saw this post but could not replicate it for the way I want

Community
  • 1
  • 1
Anuj Arora
  • 3,017
  • 3
  • 29
  • 43
  • Do you want the day of one specific forecast or the days of all forecasts of this weatherresp? Or do you want to iterate over the forecasts? – Willeke Sep 16 '15 at 15:04
  • I want to iterate over the forecasts...in each iteration I should be able to access the day attribute – Anuj Arora Sep 16 '15 at 16:30
  • You can iterate over the ordered set like in [Iterating over an NSOrderedSet](http://stackoverflow.com/questions/26603657/iterating-over-an-nsorderedset) and [Swift NSSet & CoreData](http://stackoverflow.com/questions/27091338/swift-nsset-coredata) – Willeke Sep 16 '15 at 23:19
  • I have seen both posts before but unfortunately they did not work :( – Anuj Arora Sep 17 '15 at 06:45
  • We can't help you if you don't tell us what you did and why it isn't what you want. – Willeke Sep 17 '15 at 15:11

1 Answers1

1

I had today similar issue and I solved it using this syntax:

for f in weatherResp.locForecast {
   let forecast = f as! CDForecast
   print(f.day)
}
franiis
  • 1,378
  • 1
  • 18
  • 33