简体   繁体   中英

Core data object containing NSMutableArray attribute?

:) great community here, I've read lots and found countless answers to my questions along the way. Thanks !!

Now it's question time .. I'm pretty new to iOS programming, and am working on an app which I would like to upgrade to use core data for my object storage.

My current app uses an NSMutableArray to store my objects. The objects consist of a couple of NSStrings and NSNumbers, but also each object had an NSMutableArray to store a user generated list of events containing various details that were specific to each object. The object would have methods to perform on the NSMutableArray of events, and would present the user with comparison info regarding the list of events. All was well but I had no data persistence in my program .. Enter Core Data :)

In my first few attempts to move the app into the core data world, I haven't found any clear path to twin this object storage pattern directly into a core data style object. The popup to define each attribute's type doesn't contain an NSArray, at least not that I'm finding.

I've tinkered with programming over a few years now, but am still calling myself new to object oriented programming, and have zero database experience .. Any help or experience shared will be greatly appreciated :)

Thanks !!

Unfortunately, there won't be an identical data model here and you should not use NSArray with Core Data. Also, Core Data is not like other relational databases (and it's much more than just a database), so your lack of database experience shouldn't put you at a disadvantage.

My current app uses an NSMutableArray to store my objects. The objects consist of a couple of NSStrings and NSNumbers, but also each object had an NSMutableArray to store a user generated list of events containing various details that were specific to each object. The object would have methods to perform on the NSMutableArray of events, and would present the user with comparison info regarding the list of events. All was well but I had no data persistence in my program .. Enter Core Data :)

  • The first object you allude to will become an entity .
  • The NSString(s) and NSNumber(s) will likely be attributes for that entity.
  • Event will be a separate entity with its own attributes.
  • You can relate the first entity to Event (as having one / "to one" or many / "to many" events) via a relationship.
  • Event will likely have an inverse relationship back to the first entity.
  • For your custom methods, you can assign each entity a " custom managed object class " very similar to how you are calling them now.

If you're just getting started with Core Data, I might suggest using the "Empty Application" project template in Xcode (with "use Core Data" checked) and porting your project over one view at a time. The first thing you'll want to do before porting anything though is create your data model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM