0

我正在编写一个应用程序来审查拉丁动词变位,但我遇到了障碍。我创建了一个结尾数组,但是当我尝试NSDictionary用这些结尾初始化 an 时,count字典的 the 始终为 0。我做错了什么?

这是BlackBox.h

#import <Foundation/Foundation.h>

@interface BlackBox : NSObject

@property (weak) NSDictionary *setOfEndings;

- (void)determineEndingsToUse;

@end

这是来自的相关方法BlackBox.m

- (void)determineEndingsToUse
{
NSArray *keys=[[NSArray alloc] initWithObjects:@"first person singular", @"second person singular", @"third person singular", @"first person plural", @"second person plural", @"third person singular", nil];
NSArray *endingsPossible = [[NSArray alloc] initWithObjects:@"ō", @"ās", @"at", @"āmus", @"ātis", @"ant",  nil];
NSLog(@"endingsPossible count: %d", endingsPossible.count);  //This logs 6, correctly.
if (!self.setOfEndings)
{
    self.setOfEndings = [[NSDictionary alloc] initWithObjects:endingsPossible forKeys:keys];
}
NSLog(@"setOfEndings count: %d",self.setOfEndings.count); //This logs 0 instead of 6.  Why?
}

有什么想法吗?

4

1 回答 1

1

由于 setOfEndings 是一个弱指针,它会立即被释放,因为分配的字典没有强引用。

您可以通过更改对 strong 的引用或进行如下更改来使其工作:

 NSDictionary *dict = [[NSDictionary alloc] initWithObjects:endingsPossible forKeys:keys];
 self.setOfEndings = dict;

//By default dict is a strong reference to the allocated dictionary.
于 2012-10-19T12:10:22.797 回答