Hi Greg,
When generating NSManagedObject subclasses from a Core Data model, what things should I consider when deciding to check the āuse scalar properties for primitive data typesā checkbox? I understand mechanically what the checkbox does, setting the attribute types to things like NSNumber and NSDate if not checked and to things like Bool, Int32 and NSTimeInterval if it is checked, but I donāt totally grasp the ramifications thatās going to have for me when working with the generated classes. Do you have any guidelines or thoughts on when you should check that box when generating?
Hi Jason, thatās a great question! For all-Swift projects, I definitely prefer the native types such as Bool
and Int
. That just means Core Data will handle the complexity of bridging to the actual data store while I get to write using native-feeling types in Swift.
For mixed projects that have more Objective-C, Iāll think about using the non-scalar properties. Objective-C will bridge Swift Int
types and such automatically, but sometimes I like seeing the more Obj-C style types in there.
I donāt think thereās any runtime performance penalty or anything like that with either choice. For me, itās just about the primary language and whatās more comfortable.
Thanks for the question and I hope this helps!
This does help, thanks, Greg. What threw me a bit was that Int32 types are generated instead of Swift āIntā types. Seems like you have to do some conversion no matter what if youāre working with Ints in your Swift code.
The other two things that the generator does when generating Swift subclasses is 1) always create optional types, which you mentioned in the video and 2) create NSSets for relationships rather than typed Sets. Iām manually changing these after generation, but, of course, my changes are overwritten when I re-generate as my model is still in a state of flux. Hopefully the support for both of these will be added at some point for Swift subclasses.
Hi,greg,i generating NSManagedObject subclasses ,but i canāt set the property. i just wanna access some json data and set the property. i donāt want to save the data to disk.how can i use the NSManagedObject as NSObject.i apprecaite any help
Hi Greg, at about 4:55 in the video you say youāre passing the managedObjectContext down the view stack. Youāre doing this with a tabBarController, but how can i do the same thing to pass it to a tableViewController that is accessed directly from the root view controller?
(1) Navigation controller
(2) root view controller
(3) table view controller ā I want to use it here
Thanks,
Jim
Hey greg, enjoying your Core Data tutorials. As I work through the challenge Iām trying to complete the changes in tableView(_:cellForRowAtIndexPath:) in PeopleTableViewController.swift but am unable to figure it out. I downloaded both the āchallenge finishedā and āuber challenge finishedā projects but donāt see any changes reflected in there.
I did change the declaration for āpersonā at the top to from var people = NSManagedObject to:
var people = [Person]()
Any hints on what would need to be changed in cellForRowAtIndexPath?
Hereās the code:
` override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ā UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(āPersonCellā, forIndexPath: indexPath)
let person = people[indexPath.row]
if let name = person.valueForKey("name") as? String {
cell.textLabel?.text = name
}
return cell
}`
Ok, I got brave and just forged ahead and figured it out. I replaced the followingā¦
if let name = person.valueForKey("name") as? String {
cell.textLabel?.text = name
}
ā¦ with this. I like how concise it is.
cell.textLabel?.text = people[indexPath.row].name
Hi ā¦ Iāve discovered some issue with addressLabel, which isnāt appearing on screen at all.
Iāve tried starter and completed projects for this part and they all behave the same, except completed version of challenge project ā¦ when I run this project addressLabel has appeared
So, after some trying to solve this problem Iāve found that if in class ViewController in method viewDidLoad() where we setting up:
layout.itemSize = CGSize(width: self.view.bounds.size.width/2.0 - 16, height: 250)
weāve to change the height to: 280, like so:
layout.itemSize = CGSize(width: self.view.bounds.size.width/2.0 - 16, height: 280).
As it is in completed challenge project for this part.
=> One more thing ā¦ in challenge documentation in page 4 thereāre peace of code that shouldnāt be
(just before section āImplementing the Picker Delegateā; this is the same code which is in section āShowing The Imageā, where it should be):
if let pictureData = person.picture {
cell.pictureImageView.image = UIImage(data: pictureData as Data)
}
I really didnāt get the purpose of making name attribute required Even If I am not giving the name I didnāt see any difference If I make it optional .