Value With Pattern question

I made a project with a table for recording weather information using bindings, and modeled after Shane’s Coming_To_Florida project. Everything works fine except for the Value With Pattern bindings. I am trying to bind @max.lowTemp to a label, but I always get an error saying “this class is not key value coding-compliant for the key lowTemp”. I do have @max.lowTemp in the keys list of the controller, and the label’s binding is listed as “Display Pattern Value 1 (arrayController.arrangedObjects.@max.lowTemp)”. The only one of these types of bindings that works is @count (the only one that doesn’t require something to the right of the @whatever). I can’t get @avg, @sum, or @min to work either.

Does a simple binding to arrayController.selection.lowTemp work?

I don’t have any bindings to selection (yet), but arrayController.arrangedObjects.lowTemp works.

I tried binding arrayController.selection.lowTemp to the label, and that gave me the same error:

2010-05-13 08:00:46.157 Weather[1465:a0f] An uncaught exception was raised
2010-05-13 08:00:46.157 Weather[1465:a0f] [<WeatherAppDelegate 0x20002dd20> valueForUndefinedKey:]: this class is not key value coding-compliant for the key lowTemp.
2010-05-13 08:00:46.160 Weather[1465:a0f] *** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<WeatherAppDelegate 0x20002dd20> valueForUndefinedKey:]: this class is not key value coding-compliant for the key lowTemp.’

That sugges lowTemp is the problem, and not @max.

Shane,

If that’s true then I don’t understand while the table data is bound successfully. lowTemp is one of my columns that’s bound with Value arrayController.arrangedObjects.lowTemp, and that works fine (lowTemp is the sort key for that column – I originally had it as the Identifier, and that worked also for binding that column). I also tried 2 of the other column sort keys, highTemp and inchesOfRain with @max, @sum or @avg and none of that worked. I’ve gone back and forth between your program and mine, and I can’t find any differences in coding that would explain this. The one thing that is different is in what we’re doing in our respective programs – the sum in yours seems to be counting how many rows have isAlumni checked rather than actually summing numbers in those rows. Mine is trying to average, sum, or find max of numbers in the rows. Could it be that the “numbers” aren’t actually numbers, but strings or something else? I’m not sure why that would cause the error message I’m getting though.

Email me a copy to look at.

I’ve been wondering about taking down the Florida example. There’s nothing wrong with it, but in attempting to make the Cocoa stuff bend to AS, it becomes a bit complicated and fragile in places. If I were doing something similar today, I’d take a different approach: I’d have attendees as a separate class, and do away with the separate fields for adding entries. That way there’s be almost no code.

Shane,

I don’t know whether it would be worth your time to look at it. I just remade the program with all the same bindings and now it works, at least for @sum and @avg (@min and @max are still not working, but they give me a different error message: -[NSCFString objCType]: unrecognized selector sent to instance 0x200443280
Exception detected while handling key input.)

So, I think you’re right about the fragility. The first time I wrote the program, I added the label with the Value with Pattern binding after I had gotten the rest of it working, and had messed around with it quite a bit–I wonder whether something gets “broken” after making a lot of changes. This time I added it from the beginning and it worked.

Ric

I’m not sure that’s exactly the fragility I had in mind, but yes, I think things can end up in a bit of a state. Throwing out the build folder can be cathartic, too :slight_smile:

After working on these programs for hours, a little catharsis is a good thing. :slight_smile:

Before I tried rewriting my program, I tried a little test on yours. I changed the first names, “Ray” and “Shane”, in theData to numbers, added a new label to the nib, added the key @avg.firstName to the array controller’s key list, and bound the new label to Display Pattern Value1 (Array Controller.arrangedObjects.@avg.firstName). This worked, so I went on to rewrite my project, and found that @avg worked there too. After finding that @max did not work in my program, I went back and tried it in yours, by just adding the @max.firstName key and changing the binding of the new label to that key. When you start the program, the correct maximum value is shown in that label, but when you try to add a new record, I get an error (somewhat different from the one in my program) like this:

2010-05-14 08:31:05.072 Coming to Florida[2481:a0f] -[NSCFNumber length]: unrecognized selector sent to instance 0x2004bc640
2010-05-14 08:31:05.072 Coming to Florida[2481:a0f] Exception detected while handling key input.
2010-05-14 08:31:05.072 Coming to Florida[2481:a0f] -[NSCFNumber length]: unrecognized selector sent to instance 0x2004bc640

I don’t know what this means, since I can’t find “length” in CFNumber or NSNumber (or NSData for that matter). Have you gotten @max to work before in ASOC? And, is there some easy way, without a lot of logging, to find out what object instance 0x2004bc640 is?

After Edit:

I found the problem! Looking at the file I was creating (using the plist editor), the first record created in the applicationWillFinishLaunching method shows lowTemp, highTemp and rainAmount as numbers, but subsequent records, created in the running program, show these values as strings. For some reason, the @avg binding seems to coerce these strings to numbers to compute the average, but @max doesn’t. The problem was fixed by adding number formatters to the input text fields for newLow,newHigh and newRain. Now all these values show up as numbers in the plist editor and the @max method now works.

If you look in the docs, for @avg it says: “The @avg operator uses valueForKeyPath: to get the value for each item in the receiver, converts the value to a double…” Presumably it’s the conversion to double that made it work when @max didn’t.

This is one of those cases where AS’s free-and-easy approach to typing can bite. I wonder if bindings introduce some level of loose typing.

You’ve gotta watch out for those loose types :wink:

:slight_smile:

Have you had any luck with windowWillResize_toSize_ in a window delegate? I get:

every time.

I hadn’t used that one before, but I just tried using it, and I get the same thing. I tried looking at notifications to see if anything odd was going on there, but I just got the expected NSWindowWillStartLiveResizeNotification when I clicked on the resize handle. I’ve used windowDidResize: in the past, and that works fine – it looks like this is a bug.

Ric