简体   繁体   English

不兼容的指针类型分配

[英]Incompatible pointer type assigning

I have a static dictionary which has been initialised and data has been added into it in a different.m file, now in my view controller I need that static dictionary. 我有一个静态字典已经初始化,数据已经添加到另一个.m文件中,现在在我的视图控制器中我需要静态字典。 the dictionary actually contains carrier names as key and their respective number as values so what i want top do is check what carrier the phone belongs to and then get the corresponding number. 字典实际上包含运营商名称作为密钥和它们各自的数字作为值,所以我想要做的是检查手机属于哪个运营商然后获得相应的号码。 The .m file where the static Dictionary has been formed is Config.m and it has a method that actually returns the static dictionary. 已经形成静态字典的.m文件是Config.m,它有一个实际返回静态字典的方法。

+ (NSDictionary*) getMccMncToCodeDictionary
{
    return mccMncLISDictionary;
} 

what i did in my ViewController is:"Incompatible pointer type assigning to 我在ViewController中所做的是:“指向不兼容的指针类型

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

In my console it shows 在我的控制台中显示

network number:::(null)

The warning(yellow error) i get is "Incompatible pointer type assigning to 'Config*_strong' from NSDictionary*'" in the second line of the code in the ViewController 我得到的警告(黄色错误)是在ViewController的代码的第二行中“从NSDictionary *'分配给'Config * _strong'的不兼容的指针类型”

My initLISDictionary code: 我的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];
}

Of course this will not work. 当然这不会奏效。

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

This code initiates a new Config object, however when you call the network_number you are calling a CLASS method, so its worth nothing that you initiated it before. 此代码启动一个新的Config对象,但是当您调用network_number时,您正在调用CLASS方法,因此它之前没有任何值。 You need to make it an instance method (just change the + for the - and make sure its declared on the header) so that you can call: 你需要使它成为一个实例方法(只需改变 - 的+,并确保它在标题上声明),这样你就可以调用:

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

OR you would have to make your Class Method be totally self sufficient, but im sure its not what you want. 或者你必须让你的课堂方法完全自给自足,但我确定它不是你想要的。

EDIT: 编辑:

+ (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;
}

This is the closest it can be to what you seem to be trying to do. 这是你最接近你想要做的事情。

Just use these 2 lines, ignore the init 只需使用这两行,忽略init

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

Try making the initLISDictionary method a class method and then you can do the following: 尝试将initLISDictionary方法initLISDictionary类方法,然后您可以执行以下操作:

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

However, you are also initializing mccMncLISCodeDictionary instead of mccMncLISDictionary , so you will need to straighten that out as well. 但是,您也正在初始化mccMncLISCodeDictionary而不是mccMncLISDictionary ,因此您还需要理顺它。

You should do: 你应该做:

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

since you are calling this method, whose return type is NSDictionary 因为您正在调用此方法,其返回类型为NSDictionary

+ (NSDictionary*) getMccMncToCodeDictionary
{
    return mccMncLISDictionary;
}

Those are all class methods which return static objects. 这些都是返回静态对象的类方法。

You probably want to do something like this: 你可能想做这样的事情:

NSDictionary *network_number = [Config getMccMncToLISCodeDictionary];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM