简体   繁体   中英

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. 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.

+ (NSDictionary*) getMccMncToCodeDictionary
{
    return mccMncLISDictionary;
} 

what i did in my ViewController is:"Incompatible pointer type assigning to

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

My initLISDictionary code:

- (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. 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

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:

+ (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.

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*) 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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