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​,​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.