我有一个单例类,我初始化了2个这样的数组。
- (id)init {
if (self = [super init]) {
//----------------- request Array -------------------
// obj 00
listRequestArr=[[NSMutableArray alloc]init];
NSMutableDictionary *tratype=[[NSMutableDictionary alloc]init];
[tratype setValue: [NSNumber numberWithInt:-1] forKey:@"ride_type_id"];
[tratype setValue: [NSNumber numberWithInt:0] forKey:@"request_id"];
[listRequestArr addObject:tratype];
// obj 01
NSMutableDictionary *ridename=[[NSMutableDictionary alloc]init];
[ridename setValue: @"" forKey:@"ride_name"];
[listRequestArr addObject:ridename];
// obj 02
NSMutableDictionary *rideprice=[[NSMutableDictionary alloc]init];
[rideprice setValue: @"" forKey:@"ride_price"];
[rideprice setValue: @"-1" forKey:@"ride_cid"];
[rideprice setValue: @"" forKey:@"ride_price_name"];
[listRequestArr addObject:rideprice];
// obj 03
NSMutableDictionary *reqdesc=[[NSMutableDictionary alloc]init];
[reqdesc setValue: @"" forKey:@"request_desc"];
[listRequestArr addObject:reqdesc];
// obj 04
NSMutableDictionary *modesoft=[[NSMutableDictionary alloc]init];
[modesoft setValue: @"" forKey:@"modes_str"];
[listRequestArr addObject:modesoft];
// obj 05
NSMutableDictionary *origindes=[[NSMutableDictionary alloc]init];
[origindes setValue: @"" forKey:@"origin_add"];
[origindes setValue:nil forKey:@"origin_placemark"];
[origindes setValue: @"" forKey:@"des_add"];
[origindes setValue:nil forKey:@"des_placemark"];
[listRequestArr addObject:origindes];
// obj 06
NSMutableDictionary *availability=[[NSMutableDictionary alloc]init];
[availability setValue: @"" forKey:@"ret_or_one_way"];
[availability setValue: @"" forKey:@"depdate1"];
[availability setValue: @"" forKey:@"depdate2"];
[availability setValue: @"" forKey:@"retdate1"];
[availability setValue: @"" forKey:@"retdate2"];
[listRequestArr addObject:availability];
// obj 07
NSMutableDictionary *passengers=[[NSMutableDictionary alloc]init];
[passengers setValue: @"0" forKey:@"adult"];
[passengers setValue: @"0" forKey:@"child"];
[passengers setValue: @"0" forKey:@"infants"];
[listRequestArr addObject:passengers];
requestCompletion=[[NSMutableArray alloc]init];
for (NSInteger i = 0; i < 10; i++)
[requestCompletion addObject:[NSNumber numberWithInteger:0]];
return self;
}
我的一个ViewController
内置
dm=[Datamanager sharedmanager];
dm.listRequestArr=nil;
dm.requestCompletionArr=nil;
当我完成webservice调用时会发生这种情况。完成Web服务调用之后如果我开始发出另一个请求,虽然我做了dm = [Datamanager sharedmanager]这两个数组仍然是零;似乎它没有初始化我在Datamanager
类中所做的事情。这是什么原因?请帮我。 Fisrt时间有效,但第二次没有我将它们设置为nil。
由于
答案 0 :(得分:0)
Singleton只是在app live时分配一次。在第一次调用之后它有alloc并且你再次将它设置为nil。所以第二次:它仍然是零。这很正常。你应该检查它nil并再次分配它。
答案 1 :(得分:0)
两种可能的解决方案:
1)你不需要单身,每个视图控制器都可以使用它自己,因此只设置一次。
2)您希望 DESTROY 单例实例,因此为此目的公开方法reinit()或clear()。
使用您的代码,可能的'reset()'方法是:
- (id)init {
if (self = [super init]) {
[self reset];
return self;
}
-(void) reset {
//----------------- request Array -------------------
// obj 00
listRequestArr=[[NSMutableArray alloc]init];
NSMutableDictionary *tratype=[[NSMutableDictionary alloc]init];
[tratype setValue: [NSNumber numberWithInt:-1] forKey:@"ride_type_id"];
[tratype setValue: [NSNumber numberWithInt:0] forKey:@"request_id"];
[listRequestArr addObject:tratype];
// obj 01
NSMutableDictionary *ridename=[[NSMutableDictionary alloc]init];
[ridename setValue: @"" forKey:@"ride_name"];
[listRequestArr addObject:ridename];
// obj 02
NSMutableDictionary *rideprice=[[NSMutableDictionary alloc]init];
[rideprice setValue: @"" forKey:@"ride_price"];
[rideprice setValue: @"-1" forKey:@"ride_cid"];
[rideprice setValue: @"" forKey:@"ride_price_name"];
[listRequestArr addObject:rideprice];
// obj 03
NSMutableDictionary *reqdesc=[[NSMutableDictionary alloc]init];
[reqdesc setValue: @"" forKey:@"request_desc"];
[listRequestArr addObject:reqdesc];
// obj 04
NSMutableDictionary *modesoft=[[NSMutableDictionary alloc]init];
[modesoft setValue: @"" forKey:@"modes_str"];
[listRequestArr addObject:modesoft];
// obj 05
NSMutableDictionary *origindes=[[NSMutableDictionary alloc]init];
[origindes setValue: @"" forKey:@"origin_add"];
[origindes setValue:nil forKey:@"origin_placemark"];
[origindes setValue: @"" forKey:@"des_add"];
[origindes setValue:nil forKey:@"des_placemark"];
[listRequestArr addObject:origindes];
// obj 06
NSMutableDictionary *availability=[[NSMutableDictionary alloc]init];
[availability setValue: @"" forKey:@"ret_or_one_way"];
[availability setValue: @"" forKey:@"depdate1"];
[availability setValue: @"" forKey:@"depdate2"];
[availability setValue: @"" forKey:@"retdate1"];
[availability setValue: @"" forKey:@"retdate2"];
[listRequestArr addObject:availability];
// obj 07
NSMutableDictionary *passengers=[[NSMutableDictionary alloc]init];
[passengers setValue: @"0" forKey:@"adult"];
[passengers setValue: @"0" forKey:@"child"];
[passengers setValue: @"0" forKey:@"infants"];
[listRequestArr addObject:passengers];
requestCompletion=[[NSMutableArray alloc]init];
for (NSInteger i = 0; i < 10; i++)
[requestCompletion addObject:[NSNumber numberWithInteger:0]];
}
确保添加类
的头文件-(void) reset;