不兼容的指针类型分配

时间:2013-03-15 17:55:26

标签: ios nsdictionary

我有一个静态字典已经初始化并且数据已经添加到另一个.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)

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

我的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];
}

3 个答案:

答案 0 :(得分: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;
}

这是你最接近你想要做的事情。

只需使用这两行,忽略init

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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

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

你可能想做这样的事情:

NSDictionary *network_number = [Config getMccMncToLISCodeDictionary];