如何从NSMutableArray中保存的另一个对象访问NSDocument对象? [英] How to access NSDocument object from another object held in NSMutableArray?
问题描述
问题:
我有一个NSDocument类它包含两个属性: text
(NSString)和 phrase
(NSObject的NSMutableArray与NSString在它们)。
在Doc NIB文件中,我有一个TextView(显示短语),有两列。第一列绑定到一个ArrayController并显示NSString。这工作确定。
我想计算 text
中的NSString出现次数,并显示在第二列中。
我尝试的:
在我的NSObject中定义一个指向TextView的静态var。一旦NIB被加载,它将静态var设置为包含 text
字符串的TextView。
确定如果我打开一个窗口。但是如果我尝试打开多个窗口,静态var将更新与TextView的新实例(从其他窗口)。
如何访问 text
从每个NSObject?换句话说,对象图是NSDocument - (包含一个) - > NSMutableArray - (包含多个) - > NSObject,那么如何从NSObject获取NSDocument?
<$ c $ c> NSDictionary * entry = [NSDictionary dictionaryWithObjectsAndKeys:
theP,@phrase,
theCount,@count,
nil];
[theArray addObject:entry];
绑定到 arrangedObjects.phrase
和<$或者,您可以创建一个 Phrase
。c $ c> arrangeObjects.count 。
<包含短语,对文档的引用和计算计数的代码的类。
This is really an extension of How to show calculated values in NSTableView?
The problem:
I have an NSDocument class that contains two properties: text
(NSString) and phrases
(NSMutableArray of NSObjects with NSString in them).
In the Doc NIB file I have a TextView (to display phrases) with two columns. First column is bound to an ArrayController and displays NSString. That works ok.
I want to count the number of NSString occurrences in text
and display that in the second column.
What I tried:
Define a static var in my NSObject that would point to a TextView. Once the NIB is loaded it would set this static var to a TextView that contains the text
string.
This works OK if I open a single window. But if I try opening multiple windows, the static var would get updated with the new instances of TextView (from other windows). Obviously this breaks everything.
Question:
How do I access text
from each of the NSObject? In other words, the object diagram is NSDocument --(contains one)--> NSMutableArray --(contains multiple)--> NSObject, so how do I get to NSDocument from NSObject?
Instead of an array of strings, use an array of dictionaries:
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
thePhrase, @"phrase",
theCount, @"count",
nil];
[theArray addObject:entry];
Bind to arrangedObjects.phrase
and arrangedObjects.count
.
Alternatively, you could create a Phrase
class which contains the phrase, a reference to the document, and code to calculate the count.
这篇关于如何从NSMutableArray中保存的另一个对象访问NSDocument对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!