O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Customizing Objects Created by NSArrayController

Given that your users are probably going to add cars to the system when they purchase them, it would be nice if the datePurchased attribute were set to the current date. One good way to do this is to subclass NSArrayController and override its newObject() method.

Create a new file of type Cocoa Class. It will be a subclass of NSArrayController. Name it CarArrayController. In CarArrayController.swift, override newObject:

o​v​e​r​r​i​d​e​ ​f​u​n​c​ ​n​e​w​O​b​j​e​c​t​(​)​ ​-​>​ ​A​n​y​O​b​j​e​c​t​ ​{​ ​ ​ ​ ​l​e​t​ ​n​e​w​O​b​j​ ​=​ ​s​u​p​e​r​.​n​e​w​O​b​j​e​c​t​(​)​ ​a​s​!​ ​N​S​O​b​j​e​c​t​ ​ ​ ​ ​l​e​t​ ​n​o​w​ ​=​ ​N​S​D​a​t​e​(​)​ ​ ​ ​ ​n​e​w​O​b​j​.​s​e​t​V​a​l​u​e​(​n​o​w​,​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required