Re: what do I need the NSArrayController for?

2014-04-03 Thread Seth Willits
On Apr 3, 2014, at 3:29 AM, Roland King  wrote:

> So where I ended up is understanding my model, which conflated available 
> devices and selected device was flawed thus meaning I can't have two views 
> each with a different selected device.

I meant to make that point too. In their example it makes sense for a game 
character to have a list of available weapons and a selected weapon, but in 
your case, the same object having both properties may not.



> The only odd extra I've had to write is a small class which binds to the 
> Array Controller (to get the arrangedObjects and the selectedIndexes, of 
> which there is only ever 1...

Since that class only wants to be notified, not influence the selection, KVO is 
better idea than a binding IMO. (There are those who will say both are war 
crimes, but that's besides the point.) Use bindings only if you want the both 
ends to be able to change the value.


--
Seth Willits






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-03 Thread Roland King
So where I ended up is understanding my model, which conflated available 
devices and selected device was flawed thus meaning I can't have two views each 
with a different selected device. So now that class just supports the list of 
devices. I'm using an Array Controller to mediate between that list and a table 
view, and bindings make that very easy and I don't have to read the Table View 
Programming Guide again

The only odd extra I've had to write is a small class which binds to the Array 
Controller (to get the arrangedObjects and the selectedIndexes, of which there 
is only ever 1) and vends an object representing the selected device, opened 
and working .. and closing it again if it becomes deselected. This seemed at 
the time an odd class to have to write, but that's code which would normally, 
before bindings, have been part of the table view data source/delegate huge 
block of code, using bindings just meant I needed to factor that piece out. 

I still have a long, long way to go with bindings, not least of all learning 
when NOT to use them, but this has been a very helpful discussion. 


On 3 Apr, 2014, at 8:02 am, Seth Willits  wrote:

> On Apr 2, 2014, at 4:34 PM, Roland King  wrote:
> 
>> That sent me into the documentation to find that indeed there *is* a 
>> selected object property on NSArrayController, but it's a *property* not a 
>> binding.
> 
> Note that the selection property value is a proxy which can represent a 
> single object, multiple objects, no object, etc. So don't expect it to be a 
> single instance of your class.
> 
> 
> 
>> But there's more. That diagram also shows how the selected weapon flows back 
>> to the Combatant (thus meaning a combatant can only have one selected weapon 
>> :) ). That binds the property selection.selectedWeapon to a binding called 
>> .. selectedObject. Where is that binding? I checked the documentation and 
>> can find that on pop up buttons and matrices but not on a table view nor an 
>> NSArrayController.
> 
> The weapon control is a popup, not a table view. It's showing the 
> selectedObject binding of the NSPopUpButton.
> 
> 
> 
>> That example states it's IB-only, no code required, is it just out of date? 
>> If it is, is there a way one could use bindings to accomplish what it's 
>> doing there, binding the selected object in a detail array directly back to 
>> a property on the selected object in the master? 
> 
> It's valid as is.
> 
> The popup items are the weapons the combatant has (obtained via the first 
> array controller's selection.weapons path), and then the selected item in 
> that popup is determined by binding to the combatant's selectedWeapon (via 
> the first controller's selection.selectedWeapon path)
> 
> 
> 
> --
> Seth Willits
> 
> 
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/rols%40rols.org
> 
> This email sent to r...@rols.org


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-02 Thread Seth Willits
On Apr 2, 2014, at 4:34 PM, Roland King  wrote:

> That sent me into the documentation to find that indeed there *is* a selected 
> object property on NSArrayController, but it's a *property* not a binding.

Note that the selection property value is a proxy which can represent a single 
object, multiple objects, no object, etc. So don't expect it to be a single 
instance of your class.



> But there's more. That diagram also shows how the selected weapon flows back 
> to the Combatant (thus meaning a combatant can only have one selected weapon 
> :) ). That binds the property selection.selectedWeapon to a binding called .. 
> selectedObject. Where is that binding? I checked the documentation and can 
> find that on pop up buttons and matrices but not on a table view nor an 
> NSArrayController.

The weapon control is a popup, not a table view. It's showing the 
selectedObject binding of the NSPopUpButton.



> That example states it's IB-only, no code required, is it just out of date? 
> If it is, is there a way one could use bindings to accomplish what it's doing 
> there, binding the selected object in a detail array directly back to a 
> property on the selected object in the master? 

It's valid as is.

The popup items are the weapons the combatant has (obtained via the first array 
controller's selection.weapons path), and then the selected item in that popup 
is determined by binding to the combatant's selectedWeapon (via the first 
controller's selection.selectedWeapon path)



--
Seth Willits




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-02 Thread SevenBits
On Apr 2, 2014, at 7:34 PM, Roland King  wrote:
> 
> On 3 Apr, 2014, at 12:22 am, Seth Willits  wrote:
> 
>> On Apr 2, 2014, at 6:54 AM, Roland King  wrote:
>> 
>>> At this point I realized the NSArrayController was doing nothing. ... I 
>>> took it out and bound direct to the equivalent properties on the model
>> 
>> It's not doing nothing.
>> 
>> The NSArrayController doesn't simply "forward" objects from the model to the 
>> tableview, it has its own copy of the array (the arrangedObjects) which 
>> keeps it separate the model.
>> 
>> Normally your controller object would have its own copy of the devices 
>> array, be the delegate and data source for the tableview, providing the 
>> filtered and sorted data to the tableview, handle the selection 
>> notifications and update things. Not to mention listen to when the source 
>> devices array changes to get a new copy of it.
>> 
>> NSArrayController does all of that for you, without interfering with your 
>> model.
>> 
>> By binding the table view to the model properties directly, you've now 
>> established that no other tableview or any other object can have its own 
>> selection, or order for those objects. You can't sort or filter the 
>> tableview without sorting or filtering the model data itself, which is most 
>> often a bad idea.
>> 
>> As for the selection property on your model, you can easily listen to either 
>> the tableview or the array controller's selection changing, and set the 
>> property yourself. You're not required or expected to binding everything 
>> just because you can in some manner.
>> 
> 
> Thanks for the replies thus far - they are making the picture a little 
> clearer. The point about only one tableview being able to have a selection or 
> ordering on the elements is very good, because it's not a use-case I had, I 
> didn't think about it, now I'm wondering how to split the 'list of serial 
> devices' from the 'selected serial device' in a way which works to see if I 
> can (the complication here comes from the act of being selected opening the 
> device and doing stuff and being deselected closes it again, so I need to 
> think on that a bit, perhaps KVO there is better than bindings)
> 
> I went back to the venerable Cocoa Bindings programming guide and I can see 
> where one of my expectations came from. In the 'Real World Example' under 
> 'What are Cocoa Bindings' is an example of an array of Combatants, bound to a 
> table view, and another table view showing the weapons for that combatant, 
> one of which is selected. That shows various things being bound to 
> 'selection.*', for instance Title is bound to selection.name. That sent me 
> into the documentation to find that indeed there *is* a selected object 
> property on NSArrayController, but it's a *property* not a binding. So I 
> don't need to bind the 'selection' of the NSObjectArray to something, I need 
> to bind something to the selected *property* of the NSArrayController. The 
> asymmetry and different namespace between properties and bindings bites! Well 
> it's nice to know there is such a property, that's what I needed.

Apple’s documentation isn’t always perfect. Though it’s far from the worst I’ve 
seen, it does lack a bit in areas.

> 
> But there's more. That diagram also shows how the selected weapon flows back 
> to the Combatant (thus meaning a combatant can only have one selected weapon 
> :) ). That binds the property selection.selectedWeapon to a binding called .. 
> selectedObject. Where is that binding? I checked the documentation and can 
> find that on pop up buttons and matrices but not on a table view nor an 
> NSArrayController. That example states it's IB-only, no code required, is it 
> just out of date? If it is, is there a way one could use bindings to 
> accomplish what it's doing there, binding the selected object in a detail 
> array directly back to a property on the selected object in the master? 
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/sevenbitstech%40gmail.com
> 
> This email sent to sevenbitst...@gmail.com



signature.asc
Description: Message signed with OpenPGP using GPGMail
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-02 Thread Roland King

On 3 Apr, 2014, at 12:22 am, Seth Willits  wrote:

> On Apr 2, 2014, at 6:54 AM, Roland King  wrote:
> 
>> At this point I realized the NSArrayController was doing nothing. ... I took 
>> it out and bound direct to the equivalent properties on the model
> 
> It's not doing nothing.
> 
> The NSArrayController doesn't simply "forward" objects from the model to the 
> tableview, it has its own copy of the array (the arrangedObjects) which keeps 
> it separate the model.
> 
> Normally your controller object would have its own copy of the devices array, 
> be the delegate and data source for the tableview, providing the filtered and 
> sorted data to the tableview, handle the selection notifications and update 
> things. Not to mention listen to when the source devices array changes to get 
> a new copy of it.
> 
> NSArrayController does all of that for you, without interfering with your 
> model.
> 
> By binding the table view to the model properties directly, you've now 
> established that no other tableview or any other object can have its own 
> selection, or order for those objects. You can't sort or filter the tableview 
> without sorting or filtering the model data itself, which is most often a bad 
> idea.
> 
> As for the selection property on your model, you can easily listen to either 
> the tableview or the array controller's selection changing, and set the 
> property yourself. You're not required or expected to binding everything just 
> because you can in some manner.
> 

Thanks for the replies thus far - they are making the picture a little clearer. 
The point about only one tableview being able to have a selection or ordering 
on the elements is very good, because it's not a use-case I had, I didn't think 
about it, now I'm wondering how to split the 'list of serial devices' from the 
'selected serial device' in a way which works to see if I can (the complication 
here comes from the act of being selected opening the device and doing stuff 
and being deselected closes it again, so I need to think on that a bit, perhaps 
KVO there is better than bindings)

I went back to the venerable Cocoa Bindings programming guide and I can see 
where one of my expectations came from. In the 'Real World Example' under 'What 
are Cocoa Bindings' is an example of an array of Combatants, bound to a table 
view, and another table view showing the weapons for that combatant, one of 
which is selected. That shows various things being bound to 'selection.*', for 
instance Title is bound to selection.name. That sent me into the documentation 
to find that indeed there *is* a selected object property on NSArrayController, 
but it's a *property* not a binding. So I don't need to bind the 'selection' of 
the NSObjectArray to something, I need to bind something to the selected 
*property* of the NSArrayController. The asymmetry and different namespace 
between properties and bindings bites! Well it's nice to know there is such a 
property, that's what I needed. 

But there's more. That diagram also shows how the selected weapon flows back to 
the Combatant (thus meaning a combatant can only have one selected weapon :) ). 
That binds the property selection.selectedWeapon to a binding called .. 
selectedObject. Where is that binding? I checked the documentation and can find 
that on pop up buttons and matrices but not on a table view nor an 
NSArrayController. That example states it's IB-only, no code required, is it 
just out of date? If it is, is there a way one could use bindings to accomplish 
what it's doing there, binding the selected object in a detail array directly 
back to a property on the selected object in the master? 


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-02 Thread Jerry Krinock
Some good answers have already been posted.  Just one more general comment.

Ideally, an API is designed based on the API contract.  However, Cocoa Bindings 
are deep and complex, and its API has been difficult to describe in words.  
Compared to other OS X technologies, Cocoa Bindings may therefore be based 
somewhat more on cases which have occurred in common usage patterns, and less 
on the API.  The common usage of Cocoa Bindings involves array controllers for 
arrays.  Therefore, if you do not use array controllers for arrays, you are 
more likely to run in to bugs or unexpected behaviors.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-02 Thread Seth Willits
On Apr 2, 2014, at 6:54 AM, Roland King  wrote:

> At this point I realized the NSArrayController was doing nothing. ... I took 
> it out and bound direct to the equivalent properties on the model

It's not doing nothing.

The NSArrayController doesn't simply "forward" objects from the model to the 
tableview, it has its own copy of the array (the arrangedObjects) which keeps 
it separate the model.

Normally your controller object would have its own copy of the devices array, 
be the delegate and data source for the tableview, providing the filtered and 
sorted data to the tableview, handle the selection notifications and update 
things. Not to mention listen to when the source devices array changes to get a 
new copy of it.

NSArrayController does all of that for you, without interfering with your model.

By binding the table view to the model properties directly, you've now 
established that no other tableview or any other object can have its own 
selection, or order for those objects. You can't sort or filter the tableview 
without sorting or filtering the model data itself, which is most often a bad 
idea.

As for the selection property on your model, you can easily listen to either 
the tableview or the array controller's selection changing, and set the 
property yourself. You're not required or expected to binding everything just 
because you can in some manner.


--
Seth Willits




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: what do I need the NSArrayController for?

2014-04-02 Thread Keary Suska
On Apr 2, 2014, at 7:54 AM, Roland King wrote:

> I have an array property in my model which is a list of serial devices, I 
> want to display it in a table view and let the user select one. So I 
> instantiated a tableView, an NSArrayController, bound the contents of the 
> NSArraycontroller to the serialDevices property and added a few other 
> bindings to the Table View and Table View Column; OK now I have a list of 
> devices on the screen. 
> 
> In the model I had a selectedSerialDevice property, which was the selected 
> object out of the serial device array, I wanted to bind that to the selection 
> on the table view. I could find only a Selection Indexes binding on a Table 
> View so I changed the model to have an NSIndexSet with one index representing 
> the selected serial device, which I bound through the NSArrayController and 
> then to the table view. So already my model isn't actually what I wanted, I 
> wanted a selected object, but the NSArrayController and table View want an 
> index set so now my model has an index set in it to make the other objects 
> happy. The view/viewcontroller is driving the model .. hmm. 
> 
> At this point I realized the NSArrayController was doing nothing. It was 
> forwarding my array of objects and my selected index set I didn't want to the 
> Table View. I took it out and bound direct to the equivalent properties on 
> the model, everything continued to work. My array shows, I select an object 
> and the index set I didn't want is updated in the model. 
> 
> So that led to two questions
> 
> 1) What is the point of the NSArrayController? It mirrors the bindings on the 
> Table View so it really seems to do nothing. If it were smart and converted a 
> model property like .. selected object into the index set the table view 
> wants, that would be useful, that would be what I'd usually use a Controller 
> for, to turn model-speak into view-speak, but it doesn't, it just passes 
> through. 

Because you get a certain amount of functionality "for free". Go ahead and try 
to sort your table view without an NSArrayController and see what happens...

> 2) Do I really have to change my model to present an index set for 
> 'selection' because there is no standard controller object which can turn 
> objects/selected object into something a table view can use? 

No. You could subclass NSArrayController to provide your own selected object 
binding. It's not the API's job to provide every possible behavior you would 
ever want to use, nor to provide such. It would make for a rather unwieldy API. 
I suspect, however, that there is a good performance reason to not have a 
selected object binding, as the NSController classes staunchly avoid it.

> I thought bindings were supposed to remove the need to write glue code to 
> match a model to a view, they do .. if the model matches the view .. in which 
> case the NS*Controller objects don't actually do much. 

Not really--bindings are supposed to help remove the glue code to *synchronize* 
your model and your view. You could always switch over to using the table view 
data source methods (and don't forget handling when data changes in the model 
and/or view) and see if those are easier--then you will have a more accurate 
basis of comparison

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com