• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Bindings Help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bindings Help


  • Subject: Re: Bindings Help
  • From: Ken Thomases <email@hidden>
  • Date: Tue, 23 Sep 2008 15:19:29 -0500

On Sep 23, 2008, at 1:21 PM, Quincey Morris wrote:

On Sep 23, 2008, at 10:22, Eric Lee wrote:

Using bindings, how do you add an object (a textfield's string to be specific) to a NSMutableArray so that a table view can show the object?


You need to modify the array in a KVO-compliant manner, which means the array needs to be a property of some object in your data model. Then use:

[[myDataModelObject mutableArrayValueForKey: @"myArray"] addObject: newString];

In cases where it can be arranged, it would be better to implement and then use the indexed accessors for to-many relationships.


As a guideline, if you're using KVC with a compile-time-determined key, you probably shouldn't be.


In the simplest case, the array can just be an instance variable of the data model object, since KVC is capable of finding instance variables directly:

	@interface MyDataModel ... {
		NSMutableArray* myArray;
		...
	}

I recommend avoiding this. The ability of KVC to access instance variables directly should be thought of as a last resort fallback. Any newly designed code should use accessors. Otherwise, you're violating encapsulation -- other code can change the state of your object behind its back. Personally, I recommend that any classes you implement should override +accessInstanceVariablesDirectly to return NO so you get errors early in the development process if you forget to provide accessors.



Or it can be a genuine property:

	@interface MyDataModel ... {
	...
	}
	@property (...) NSArray* myArray;

with a suitable implementation in MyDataModel.

Two things:

1) If you don't provide the indexed accessors, then KVC will have no choice but to fully replace the array (with -setMyArray:) every time there's an attempt to mutate this property. This is inefficient. It also deprives your class and any KVO observers of the more fine- grained ability to detect what's really being changed.

2) A "genuine" property is any property for which there are accessors of the proper form. It doesn't necessarily require the use of Objective-C 2.0's declared property feature. (Not intended as a criticism, just additional explanation.)

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Bindings Help (From: Eric Lee <email@hidden>)
 >Re: Bindings Help (From: Quincey Morris <email@hidden>)

  • Prev by Date: Fwd: Memory cleanup when init fails?
  • Next by Date: Menu item issues when programmatically updating an application's main menu
  • Previous by thread: Re: Bindings Help
  • Next by thread: Naming alert panel buttons.
  • Index(es):
    • Date
    • Thread