开发者

NSArraycontroller access of content

开发者 https://www.devze.com 2023-02-06 01:17 出处:网络
I am connecting an arraycontroller (myArraycontroller) in Interface Builder to an array (fileDictionariesArray) consisting of dictionaries. This works fine, but when I try to access and enumerate over

I am connecting an arraycontroller (myArraycontroller) in Interface Builder to an array (fileDictionariesArray) consisting of dictionaries. This works fine, but when I try to access and enumerate over the contents of the arraycontroller [myArrayController arrangedObjects] I get nil in content until I add the content programmatically to my arrayController like this:

[myArrayController addObjects:fileDictionariesArray];

After I have done that I can loop over all the contents. The problem is that the array (fileDictionariesArray) has been added two times. One time through IB binding and one time through addObjects. I just can't access the arrayContoller until I set the content programmatically. I can delete the content of the arrayController and then set it programmatically again like this:

[myArrayController setContent:nil];
[myArrayController addObjects:fileDictionariesArray];

Which gives the correct number of items in the arrayController, but it does not seem like the correct way to do this. I would appreciate i开发者_开发问答t of anyone could give me a hint on how to access my arrayController through the arrangedObjects array without adding the content two times.

Thank you for your help. Cheers, Trond Kristiansen


Based on your post and the comments between you and Bavarious, I'm convinced he shouldn't have deleted his answer. He'd asked you where you were calling this code (from -awakeFromNib?). This is an essential question to answer.

If you call this from -awakeFromNib, the next question is "of what object in the nib? the owner? some other controller? what is the? a document?"

The thing is the array controller likely doesn't have time to observe the "changes" to the array before you call your code. That's why adding the content forcibly results in its being added twice (because after you're done, the array controller observes its content array and pulls in the content ... again ... at which point you've doubled the array's contents).

If you're doing immediate startup stuff, it might be best to operate directly on the array. There's nothing "wrong" or "dirty" about that, so long as you leave responsibility with the array controller after startup is complete.

It's hard to get any more specific because I'm just guessing at what you might be doing. Amend your question to include a more complete description of how/where your array controller fits into the architecture, what specifically you're doing to the array at startup, etc. Missing any of this information forces too many guesses.

0

精彩评论

暂无评论...
验证码 换一张
取 消