我正在编写一个应用来回顾拉丁语动词变形,但我遇到了麻烦。我创建了一个结尾数组,但是当我尝试使用这些结尾初始化NSDictionary
时,字典的count
始终为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?
}
有什么想法吗?
答案 0 :(得分:1)
由于setOfEndings是一个弱指针,它会立即释放,因为没有对分配字典的强引用。
您可以通过更改对强的引用或更改如下来使其工作:
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:endingsPossible forKeys:keys];
self.setOfEndings = dict;
//By default dict is a strong reference to the allocated dictionary.