0

To watch a array I do like this:

NSMutableArray *myArray = [@[] mutableCopy];
// set a lldb watchpoint here: (lldb) w s v myArray

However this kind of watchpoint ONLY WORKS when the memory address change eg:

myArray = [NSMutableArray array];

What I want to watch is myArray's content change eg:

[myArray addObject: @1] //I want get notify when this thing happen

Is anybody face the same problem, How do you achieve this kind of thing.

liaa
  • 98
  • 1
  • 7
  • Maybe derive a class from NSArray and overwrite method `addObject:` where You call `[super addObject:...];` and put breakpoint in there. – riodoro1 Apr 10 '15 at 09:47
  • @riodoro1 This works. But every time I do watch I will change my array class first. – liaa Apr 10 '15 at 09:55
  • Only the declaration as the interface would not change. If You want to watch all of them just `#define NSArray WatchableNSArray` – riodoro1 Apr 10 '15 at 09:58
  • I got your idea. I mean If I want watch `array1` I have to switch `array1`'s class to `WatchableNSArray` first, this is cumbersome if there are some lldb features originally support it. – liaa Apr 10 '15 at 10:10
  • @liaa, please see the link below. The full solution with the code for observing the count of mutable array is there. – malex Apr 10 '15 at 10:12
  • I have checked that out, It is a solution to this question, and more complex than @riodoro1. But before I can get the end, I can't make it the best answer. – liaa Apr 10 '15 at 10:17
  • @riodoro1, Apple strongly prevent from subclussing of any clasters like Array and other collections. – malex Apr 11 '15 at 09:21

1 Answers1

0

You need use KVO machinery for mutable collections via proxy object. All details you can find in the documentation on KVC/KVO by Apple.

The the example and details of such observation are described here

The key point is to make a proxy object for NSArray by means of

- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key

and then to use this mutable object for changing original array.

Community
  • 1
  • 1
malex
  • 9,874
  • 3
  • 56
  • 77
  • 1
    This approach can be used to observe changes to a KVO compliant property—not to any mutable array. Arrays themselves are not KVO compliant in any way. – Nikolai Ruhe Apr 10 '15 at 10:04
  • @NikolaiRuhe, This approach is used for any mutable collections. You shouldn't care about the content of your array. It is a kind of swizzling when you use proxy object to wrap initial work to some additional work around it. See Apple's documentation on to-many relations in KVC/KVO. My approach is 100% solution for the question above and I think your mark of the answer here in the direction of our discussion is not eligible. – malex Apr 10 '15 at 10:08
  • @NikolaiRuhe Of course they are KVO compliant as documented. – Amin Negm-Awad Apr 10 '15 at 11:08
  • @AminNegm-Awad Please provide a link. I'm under the impression Cocoa collection classes do not expose any KVO compliant properties. – Nikolai Ruhe Apr 10 '15 at 12:41
  • @NikolaiRuhe, the link in my answer above. There you can find full working example of using kvo with array to observe any changes. The example shows count changes. I'm using it in all my projects for a year. – malex Apr 10 '15 at 12:45
  • @malex The `mutableArrayValueForKey` mechanism can be used on any object that exposes a KVO compliant to-many relationship. In this question there is no such object. We just have a single `NSMutableArray` which, by itself, is not KVO compliant. Also, if there would be this other object that exposes the KVO compliant relationship, observers would only be notified of changes to it if they uses the KVC compliant accessors. Since the OP seems to want to break on *any* change to a given array instance this is not a feasible approach. – Nikolai Ruhe Apr 10 '15 at 12:48
  • @NikolaiRuhe, from documentation: "Given a key that identifies an _ordered_ to-many relationship, return a mutable array that provides read-write access to the related objects. Objects added to the mutable array will become related to the receiver, and objects removed from the mutable array will become unrelated." – malex Apr 10 '15 at 13:24
  • Anyway, here we don't try to approve any KVC compliance, we try to ask the question of @liaa. The task is to have a notification on change of some array collection. And the discussed approach is the true solution. I tried to find it on SO for two years. And finally wrote the own solution. – malex Apr 10 '15 at 13:26
  • @NikolaiRuhe, if you have another solution which is more compliant with KVC/KVO, I'd be glad to see it and use it. – malex Apr 10 '15 at 13:29
  • 1
    @malex What I'm trying to make clear is that KVO just does not provide a solution to this question. Your answer does not give one (it makes to-many relationships observable, not NSMutableArray contents). – Nikolai Ruhe Apr 10 '15 at 14:02
  • @NikolaiRuhe, I don't really understand what is the real difference in application cases. The task is to organize collection storage of objects and using KVO machinery somehow to receive notification on change of that collection. Anyway in the interface it looks like using NSMutableDictionary and is based on KVO. – malex Apr 10 '15 at 17:14
  • @NikolaiRuhe Okay, I understood your comment that way that it is impossible to observe to-many relationships (modeled with an array) at all. To the Q&A: Simple put the array into an instance that is accessible via KVC. (To make talking easier, let's call it an "entity" in contrast to "collections" and other "containers" as strings.) The problem I have with the answer is another one: The OP did not want to get a notification in his code, but a break in the debugger. Therefore it should not be reflected in code at all. I'm pretty sure that it is possible with some Bps, but seems to me complex. – Amin Negm-Awad Apr 11 '15 at 04:21