gpt4 book ai didi

ios - 不兼容的指针类型分配

转载 作者:行者123 更新时间:2023-12-01 19:12:38 25 4
gpt4 key购买 nike

我有一个已初始化的静态字典,并且数据已添加到不同的.m 文件中,现在在我的 View Controller 中我需要该静态字典。
字典实际上包含运营商名称作为键,它们各自的号码作为值,所以我最想做的是检查手机所属的运营商,然后获取相应的号码。
已形成静态字典的 .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];
}

最佳答案

当然,这是行不通的。

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

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

关于ios - 不兼容的指针类型分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15439097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com