Mac Developer Library

Developer

AppKit Framework Reference NSObjectController Class Reference

Options
Deployment Target:

On This Page
Language:

NSObjectController

NSObjectController is a Cocoa bindings-compatible controller class. Properties of the content object of instances of this class can be bound to user interface elements to access and modify their values.

By default, the content of an NSObjectController instance is an NSMutableDictionary object. This allows a single NSObjectController instance to be used to manage many different properties referenced by key-value paths. The default content object class can be changed by calling setObjectClass:, which subclasses must override. Your application should use a custom data class that is key-value compliant whenever possible.

Object Controllers, Entity Mode, and Lazy Fetching

NSObjectController and its subclasses, when in entity mode, can now fetch lazily. With lazy fetching enabled using the method setUsesLazyFetching:, the controller will try to fetch only a small amount of data from available persistent stores. This can provide a significant improvement in memory use when a large amount of content is stored on disk but just a subset of that data is required in memory.

When set to use lazy fetching, a controller will fetch objects in batches. You can change the default batch size for your application by setting a value for the the user default "com.apple.CocoaBindings.LazyFetchBatchSize". If you have table views bound to an array controller set to use lazy fetching, the size of the controller's batch size will grow as the table views' visible row count grows.

Add, Insert, and Remove operations on controllers that use lazy fetching behave similarly to the same operations on a regular controller. The difference is that it is faster to sort an array controller using lazy fetching if:

  • All of the keys in the sortDescriptors array are modeled, non transient properties.

  • All of the selectors in the sortDescriptors array are compare: or caseInsensitiveCompare:.

  • There are no changes in the controller's managed object context

Conforms To


Import Statement


Swift

import AppKit

Objective-C

@import AppKit;

Availability


Available in OS X v10.3 and later.
  • init(content:) - initWithContent: Designated Initializer

    Initializes and returns an NSObjectController object with the given content.

    Declaration

    Swift

    init(content content: AnyObject?)

    Objective-C

    - (instancetype)initWithContent:(id)content

    Parameters

    content

    The content for the receiver.

    Return Value

    The initialized object controller, with its content object set to content.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Sets the receiver’s content object.

    Declaration

    Swift

    var content: AnyObject?

    Objective-C

    @property(strong) id content

    Parameters

    content

    The content object for the receiver.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

    See Also

    – content

  • Returns the receiver’s content object.

    Declaration

    Swift

    var content: AnyObject?

    Objective-C

    @property(strong) id content

    Return Value

    The receiver’s content object.

    Discussion

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

    See Also

    – setContent:

  • Sets whether the receiver automatically creates and inserts new content objects automatically when loading from a nib file.

    Declaration

    Swift

    var automaticallyPreparesContent: Bool

    Objective-C

    @property BOOL automaticallyPreparesContent

    Parameters

    flag

    A flag that specifies whether the receiver automatically prepares its content.

    Discussion

    If flag is YEStrue and the receiver is not using a managed object context, prepareContent is used to create the content object. If flag is YEStrue and a managed object context is set, the initial content is fetched from the managed object context using the current fetch predicate. The default is NOfalse.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Returns a Boolean value that indicates whether the receiver automatically prepares its content when it is loaded from a nib.

    Declaration

    Swift

    var automaticallyPreparesContent: Bool

    Objective-C

    @property BOOL automaticallyPreparesContent

    Return Value

    YEStrue if the receiver automatically prepares its content when loaded from a nib, otherwise NOfalse.

    Discussion

    See setAutomaticallyPreparesContent: for a full explanation of "automatically prepares content."

    The default is NOfalse.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Typically overridden by subclasses that require additional control over the creation of new objects.

    Declaration

    Swift

    func prepareContent()

    Objective-C

    - (void)prepareContent

    Discussion

    Subclasses that implement this method are responsible for creating the new content object and setting it as the receiver’s content object. This method is only called if automaticallyPreparesContent has been set to YEStrue.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Sets the object class to use when creating new objects.

    Declaration

    Swift

    var objectClass: AnyClass!

    Objective-C

    @property(assign) Class objectClass

    Parameters

    objectClass

    The object class to use when creating new objects.

    Discussion

    NSObjectController's default implementation assumes that instances of objectClass are initialized using a standard init method that takes no arguments.

    If an entity name has been set (see setEntityName:), this method has no effect.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Returns the class used when creating new non-Core Data objects.

    Declaration

    Swift

    var objectClass: AnyClass!

    Objective-C

    @property(assign) Class objectClass

    Return Value

    The object class used when creating new non-Core Data objects (that is, if no entity has been set)—see newObject.

    Discussion

    If an entity has been set, then the class returned by this method does not automatically reflect the class for the entity.

    The default class is NSMutableDictionary.

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Creates and returns a new object of the appropriate class.

    Declaration

    Swift

    func newObject() -> AnyObject

    Objective-C

    - (id)newObject

    Return Value

    A new object of the appropriate class. The returned object is implicitly retained, the sender is responsible for releasing it (with either release or autorelease).

    If an entity name is set (see setEntityName:), the object created is an instance of the class specified for that entity (and the object is inserted into the receiver's managed object context). Otherwise the object created is an instance of the class returned by objectClass.

    Discussion

    This method is called when adding and inserting objects if automaticallyPreparesContent is YEStrue.

    The default implementation assumes the class returned by objectClass has a standard init method without arguments. If the object class being controlled is NSManagedObject (or a subclass thereof) its designated initializer (initWithEntity:insertIntoManagedObjectContext:) is called instead, using the entity and managed object context specified for the receiver.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Sets the receiver’s content object.

    Declaration

    Swift

    func addObject(_ object: AnyObject)

    Objective-C

    - (void)addObject:(id)object

    Parameters

    object

    The content object for the receiver.

    Discussion

    If the receiver's content is bound to another object or controller through a relationship key, the relationship of the “master” object is changed.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Removes a given object from the receiver’s content.

    Declaration

    Swift

    func removeObject(_ object: AnyObject)

    Objective-C

    - (void)removeObject:(id)object

    Parameters

    object

    The object to remove from the receiver.

    Discussion

    If object is the receiver’s content object, the receiver’s content is set to nil. If the receiver's content is bound to another object or controller through a relationship key, the relationship of the ‘master’ object is cleared.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

    See Also

    – addObject:

  • Creates a new object and sets it as the receiver’s content object.

    Declaration

    Swift

    func add(_ sender: AnyObject?)

    Objective-C

    - (void)add:(id)sender

    Parameters

    sender

    Typically the object that invoked this method.

    Discussion

    Creates a new object of the appropriate entity (specified by entityName) or class (specified by objectClass)—see newObject—and sets it as the receiver’s content object using addObject:.

    Special Considerations

    Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Returns a Boolean value that indicates whether an object can be added to the receiver using add:.

    Declaration

    Swift

    var canAdd: Bool { get }

    Objective-C

    @property(readonly) BOOL canAdd

    Return Value

    YEStrue if an object can be added to the receiver using add:, otherwise NOfalse.

    Discussion

    Bindings can use this method to control the enabling of user interface objects.

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Removes the receiver’s content object.

    Declaration

    Swift

    func remove(_ sender: AnyObject?)

    Objective-C

    - (void)remove:(id)sender

    Parameters

    sender

    Typically the object that invoked this method.

    Discussion

    Removes the receiver’s content object using removeObject:.

    Special Considerations

    Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Returns a Boolean value that indicates whether an object can be removed from the receiver.

    Declaration

    Swift

    var canRemove: Bool { get }

    Objective-C

    @property(readonly) BOOL canRemove

    Return Value

    YEStrue if an object can be removed from the receiver using remove:, otherwise NOfalse.

    Discussion

    Bindings can use this method to control the enabling of user interface objects.

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Sets whether the receiver allows adding and removing objects.

    Declaration

    Swift

    var editable: Bool

    Objective-C

    @property(getter=isEditable) BOOL editable

    Parameters

    flag

    YEStrue if the the receiver should allow adding and removing objects, otherwise NOfalse.

    Discussion

    The default is YEStrue.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

    See Also

    – isEditable

  • isEditable - isEditable Available in OS X v10.3 through OS X v10.9

    Returns a Boolean value that indicates whether the receiver allows adding and removing objects.

    Declaration

    Objective-C

    - (BOOL)isEditable

    Return Value

    YEStrue if the receiver allows adding and removing objects, otherwise NOfalse.

    Discussion

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.3 through OS X v10.9.

  • Returns the entity name used by the receiver to create new objects.

    Declaration

    Swift

    var entityName: String?

    Objective-C

    @property(copy) NSString *entityName

    Return Value

    The entity name used by the receiver to create new objects.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

  • Sets the entity name used by the receiver to create new objects.

    Declaration

    Swift

    var entityName: String?

    Objective-C

    @property(copy) NSString *entityName

    Parameters

    entityName

    The entity name used by the receiver to create new objects.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

    See Also

    – entityName

  • Causes the receiver to fetch the data objects specified by the entity name and fetch predicate.

    Declaration

    Swift

    func fetch(_ sender: AnyObject?)

    Objective-C

    - (void)fetch:(id)sender

    Parameters

    sender

    Typically the object that invoked this method.

    Special Considerations

    Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

  • Sets whether the receiver uses lazy fetching.

    Declaration

    Swift

    var usesLazyFetching: Bool

    Objective-C

    @property BOOL usesLazyFetching

    Parameters

    enabled

    Boolean value that indicates whether the receiver uses lazy fetching.

    Discussion

    When enabled the controller uses a number of techniques that typically make managing large data sets more efficient. As with all optimizations, you should use suitable performance analysis tools (such as Instruments) to determine the best solution.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns a Boolean indicating whether the receiver uses lazy fetching.

    Declaration

    Swift

    var usesLazyFetching: Bool

    Objective-C

    @property BOOL usesLazyFetching

    Return Value

    YEStrue if the receiver uses lazy fetching, otherwise NOfalse.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the default fetch request used by the receiver.

    Declaration

    Swift

    func defaultFetchRequest() -> NSFetchRequest

    Objective-C

    - (NSFetchRequest *)defaultFetchRequest

    Return Value

    The default NSFetchResult used by the receiver.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.

  • Returns the receiver’s fetch predicate.

    Declaration

    Swift

    var fetchPredicate: NSPredicate?

    Objective-C

    @property(strong) NSPredicate *fetchPredicate

    Return Value

    The receiver’s fetch predicate.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

  • Sets the receiver’s fetch predicate.

    Declaration

    Swift

    var fetchPredicate: NSPredicate?

    Objective-C

    @property(strong) NSPredicate *fetchPredicate

    Parameters

    predicate

    The fetch predicate for the receiver.

    Discussion

    The receiver uses predicate when fetching its content, for example in fetch:. If you need to customize the fetching behavior further, you can override fetchWithRequest:merge:error:.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

  • Returns the receiver’s managed object context.

    Declaration

    Swift

    var managedObjectContext: NSManagedObjectContext?

    Objective-C

    @property(strong) NSManagedObjectContext *managedObjectContext

    Return Value

    The receiver’s managed object context.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

  • Sets the receiver’s managed object context.

    Declaration

    Swift

    var managedObjectContext: NSManagedObjectContext?

    Objective-C

    @property(strong) NSManagedObjectContext *managedObjectContext

    Parameters

    managedObjectContext

    The managed object context for the receiver.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

  • Subclasses should override this method to customize a fetch request, for example to specify fetch limits.

    Declaration

    Swift

    func fetchWithRequest(_ fetchRequest: NSFetchRequest?, merge merge: Bool, error error: NSErrorPointer) -> Bool

    Objective-C

    - (BOOL)fetchWithRequest:(NSFetchRequest *)fetchRequest merge:(BOOL)merge error:(NSError **)error

    Parameters

    fetchRequest

    The fetch request to use for the fetch. Pass nil to use the default fetch request.

    merge

    If YEStrue, the receiver merges the existing content with the fetch result, otherwise the receiver replaces the entire content with the fetch result.

    error

    If an error occurs, upon return contains an NSError object that describes the problem.

    Return Value

    YEStrue if the fetch completed successfully, otherwise NOfalse.

    Discussion

    This method performs a number of actions that you cannot reproduce. To customize this method, you should therefore create your own fetch request and then invoke super’s implementation with the new fetch request.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.4 and later.

    See Also

    – fetch:

  • Returns an array of all objects to be affected by editing.

    Declaration

    Swift

    var selectedObjects: [AnyObject] { get }

    Objective-C

    @property(readonly, copy) NSArray *selectedObjects

    Return Value

    An array of all objects to be affected by editing. If the receiver supports a selection mechanism, the array contains key value coding compliant proxies of the selected objects; otherwise proxies for all content objects are returned. If the receiver is a concrete instance of NSObjectController, returns an array containing the receiver’s content object.

    Discussion

    You should avoid registering for key-value observing changes for key paths that pass through this method, (for example, selectedObjects.firstName). Using the proxy returned by the selection method is better for performance.

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

    See Also

    – selection

  • Returns a proxy object representing the receiver’s selection.

    Declaration

    Swift

    var selection: AnyObject { get }

    Objective-C

    @property(readonly, strong) id selection

    Return Value

    A proxy object representing the receiver’s selection. This object is fully key-value coding compliant, but note that it is a proxy and so does not provide the full range of functionality that might be available in the source object.

    Discussion

    If a value requested from the selection proxy using key-value coding returns multiple objects, the controller has no selection, or the proxy is not key-value coding compliant for the requested key, the appropriate marker (NSMultipleValuesMarker, NSNoSelectionMarker or NSNotApplicableMarker) is returned. Otherwise, the value of the key is returned.

    This property is observable using key-value observing.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.3 and later.

  • Returns whether the receiver can handle the action method for a user interface item.

    Declaration

    Swift

    func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool

    Objective-C

    - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item

    Parameters

    item

    The user interface item to validate. You can send item the action and tag messages.

    Return Value

    YEStrue if the receiver can handle the action method; NOfalse if it cannot.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.5 and later.