1

我有一个已初始化的静态字典,并且数据已添加到不同的.m 文件中,现在在我的视图控制器中我需要该静态字典。字典实际上包含运营商名称作为键,它们各自的号码作为值,所以我最想做的是检查手机所属的运营商,然后获取相应的号码。已形成静态字典的 .m 文件是 Config.m,它有一个实际返回静态字典的方法。

+ (NSDictionary*) getMccMncToCodeDictionary
{
    return mccMncLISDictionary;
} 

我在 ViewController 中所做的是:“分配给不兼容的指针类型

Config* network_number = [[Config alloc] init];
network_number = [Config getMccMncToLISCodeDictionary];
NSLog(@"network number:::%@", network_number);

在我的控制台中显示

network number:::(null)

我得到的警告(黄色错误)是 ViewController 代码第二行中的“不兼容的指针类型从 NSDictionary* 分配给 'Config*_strong'”

我的 initLISDictionary 代码:

- (void) initLISDictionary
{ 
NSString* MCC = @"520";
NSString* CAT3G = [NSString stringWithFormat:@"%@00",MCC];
NSString* AIS = [NSString stringWithFormat:@"%@01",MCC];
NSString* CAT_CDMA =[NSString stringWithFormat:@"%@02",MCC];
NSString* TOT3G = [NSString stringWithFormat:@"%@15",MCC];
NSString* DTAC = [NSString stringWithFormat:@"%@18",MCC];
NSString* AIS_GSM_1800 = [NSString stringWithFormat:@"%@23",MCC];
NSString* TRUE_MOVE_H = [NSString stringWithFormat:@"%@88",MCC];
NSString* TRUE_MOVE = [NSString stringWithFormat:@"%@99",MCC];

mccMncLISCodeDictionary = [NSMutableDictionary dictionary];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:2] forKey:CAT3G];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:1] forKey:AIS];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:2] forKey:CAT_CDMA];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:4] forKey:TOT3G];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:3] forKey:DTAC];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:1] forKey:AIS_GSM_1800];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:5] forKey:TRUE_MOVE];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:5] forKey:TRUE_MOVE_H];
}
4

3 回答 3

2

当然,这是行不通的。

Config* network_number = [[Config alloc] init];
network_number = [Config getMccMncToLISCodeDictionary];
NSLog(@"network number:::%@", network_number);

此代码启动一个新的 Config 对象,但是当您调用 network_number 时,您正在调用一个 CLASS 方法,因此您之前启动它毫无价值。您需要将其设为实例方法(只需更改 + 的 - 并确保其在标题中声明),以便您可以调用:

Config* network_number = [[Config alloc] init];
network_number = [network_number getMccMncToLISCodeDictionary];

或者你必须让你的类方法完全自给自足,但我确定它不是你想要的。

编辑:

+ (NSDictionary*) getMccMncToCodeDictionary
{

NSString* MCC = @"520";
NSString* CAT3G = [NSString stringWithFormat:@"%@00",MCC];
NSString* AIS = [NSString stringWithFormat:@"%@01",MCC];
NSString* CAT_CDMA =[NSString stringWithFormat:@"%@02",MCC];
NSString* TOT3G = [NSString stringWithFormat:@"%@15",MCC];
NSString* DTAC = [NSString stringWithFormat:@"%@18",MCC];
NSString* AIS_GSM_1800 = [NSString stringWithFormat:@"%@23",MCC];
NSString* TRUE_MOVE_H = [NSString stringWithFormat:@"%@88",MCC];
NSString* TRUE_MOVE = [NSString stringWithFormat:@"%@99",MCC];

mccMncLISCodeDictionary = [NSMutableDictionary dictionary];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:2] forKey:CAT3G];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:1] forKey:AIS];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:2] forKey:CAT_CDMA];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:4] forKey:TOT3G];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:3] forKey:DTAC];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:1] forKey:AIS_GSM_1800];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:5] forKey:TRUE_MOVE];
[mccMncLISCodeDictionary setValue:[NSNumber numberWithInt:5] forKey:TRUE_MOVE_H];

return mccMncLISDictionary;
}

这是最接近您似乎正在尝试做的事情。

只需使用这 2 行,忽略 init

NSMutableDictionary *network_number = [Config getMccMncToLISCodeDictionary];
NSLog(@"network number:::%@", network_number);
于 2013-03-15T19:05:02.993 回答
1

尝试使该initLISDictionary方法成为类方法,然后您可以执行以下操作:

+ (NSDictionary*) getMccMncToCodeDictionary
{
    if (mccMncLISDictionary == nil) {
        [Config initLISDictionary];
    }
    return mccMncLISDictionary;
}

但是,您也在初始化mccMncLISCodeDictionary而不是mccMncLISDictionary,因此您也需要理顺它。

你应该做:

NSDictionary *network_number = [Config getMccMncToLISCodeDictionary];
NSLog(@"network number:::%@", network_number);

因为你正在调用这个方法,它的返回类型是 NSDictionary

+ (NSDictionary*) getMccMncToCodeDictionary
{
    return mccMncLISDictionary;
}
于 2013-03-15T18:23:17.497 回答
0

这些都是返回静态对象的类方法。

你可能想做这样的事情:

NSDictionary *network_number = [Config getMccMncToLISCodeDictionary];
于 2013-03-15T19:26:07.230 回答