Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Juan Pablo Lazcano
Juan Pablo Lazcano
11,267 Points

i need my app to be compatible with ios 9+ but the class NSPersistentContainer is only available for ios 10

  @available(iOS 10.0, *)
  private lazy var persistentContainer: NSPersistentContainer = {

  }()

Xcode is adding this, but in iOS 9 is crashing

1 Answer

Nathan F.
Nathan F.
30,773 Points

If you need to support iOS 9, you will need to manage CoreData without NSPersistentContainer (the old way), since it is only available in iOS 10 and beyond. The alternative would be to add a check for availability -- if its iOS 10 or greater, use persistentContainer. If iOS 9, use some other storage method. You can do an availability check this way:

if #available(iOS 10.0, *) {
    // use persistent container
} else {
    // use another storage method
}