0

我有一个 sharedInstance ,它由许多 NSMutableArrays 组成,其中包含我的所有表单数据,这些数据分布在我的应用程序中的许多视图中。

他们在 .h 中的声明示例如下所示

@property (atomic, retain) NSMutableArray *i_date;
@property (atomic, retain) NSMutableArray *i_tailNumber;
@property (atomic, retain) NSMutableArray *i_pic;
@property (atomic, retain) NSMutableArray *i_sic;

并在 .m (-init) 中

self = [super init];

_i_date = [NSMutableArray array];
_i_tailNumber = [NSMutableArray array];
_i_pic = [NSMutableArray array];
_i_sic = [NSMutableArray array];

共享实例函数是

+(id) sharedInstance
{    
    static id sharedInstance = nil;
    if (sharedInstance == nil) {
        sharedInstance = [[self alloc] init];        
    }
    return sharedInstance;
}

编辑:

我在第一个视图上用空字符串实例化所有数组,如下所示

[_fd.i_date addObject:@""];
[_fd.i_tailNumber addObject:@""];

最后,我在为 segue 功能做准备时以这种方式与我的数据交互

[_fd.i_date replaceObjectAtIndex:leg withObject:transferDate];
rinse and repeate

我的问题是,我今天通过阅读各种博客、文档和 SO 上的所有精彩资源花了很多时间进行调查,当我转到另一个视图时,我的数组会丢失我放置在其中的任何对象。

当我点击上面的代码行时,我得到了经典

signal SIGABRT

错误,它抱怨我的数组是空的(0 个对象),所以它不能替换任何东西。

我怀疑我没有正确处理我的共享实例,而且……也许,我的数组的多个实例正在被分配,但我确信我的理解已经转变了。

任何想法正在发生什么?我想我提供了所有必要的代码片段,但如果我忘记了什么,就问吧。再次感谢!

杰西


编辑: 你们很快!:DI 在启用 ARC 的情况下创建了我的 OBJ-C 文件,所以我想我正在使用它。我是一个 C++ 人,主要是所以这个可选的手拿着 mem 的东西对我来说本来就模棱两可。


编辑: 完整的错误是

2013-04-10 20:25:03.239 ProjectName[4230:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x1caf012 0x10ece7e 0x1c65ba9 0x7c7e 0x478b87 0x478c14 0x1100705 0x342c0 0x270a64 0x1100705 0x342c0 0x34258 0xf5021 0xf557f 0xf46e8 0x63cef 0x63f02 0x41d4a 0x33698 0x1c0adf9 0x1c0aad0 0x1c24bf5 0x1c24962 0x1c55bb6 0x1c54f44 0x1c54e1b 0x1c097e3 0x1c09668 0x30ffc 0x222d 0x2155)
libc++abi.dylib: terminate called throwing an exception
4

4 回答 4

1

假设您没有使用 ARC,则您的init方法使用了糟糕的内存管理。由于您直接访问 ivars(而不是属性),因此您需要保留数组:

_i_date = [[NSMutableArray alloc] init];
_i_tailNumber = [[NSMutableArray alloc] init];
_i_pic = [[NSMutableArray alloc] init];
_i_sic = [[NSMutableArray alloc] init];
于 2013-04-10T23:42:48.770 回答
1

您没有使用这些属性,而是创建了自动释放的对象。

要么使用像这样的属性

+(id) sharedInstance
{    
    static id sharedInstance = nil;
    if (sharedInstance == nil) {
        sharedInstance = [[self alloc] init];   

         sharedInstance.i_date = [NSMutableArray array];
         sharedInstance.i_tailNumber = [NSMutableArray array];
         sharedInstance.i_pic = [NSMutableArray array];
         sharedInstance.i_sic = [NSMutableArray array];     
    }
    return sharedInstance;
}

或者按照 rmaddy 的建议去做。


[_fd.i_date replaceObjectAtIndex:leg withObject:transferDate];

此行崩溃,因为数组尚未在索引处有对象leg

尝试

[_fd.i_date addObject:transferDate];
于 2013-04-10T23:44:44.440 回答
0

您应该分配并初始化您NSMutableArray的 s:

_i_date = [[NSMutableArray alloc] init];

然后使用你的单例类,即:

[[_fd sharedInstance] i_date] addObject...]
于 2013-04-10T23:44:48.600 回答
0

不知道的是,我使用了函数“didMoveToParentViewController”,如果新视图重载并被视图使用,则在显示新视图后调用该函数。在其中,我正在清除所有数组,认为只有在返回父视图时才会调用它,因为“did”这个词是过去时态的词。由于情况并非如此,我在页面加载后立即清除我的数组,这导致在 segues 期间出现空数组。

于 2013-04-23T13:22:56.293 回答