【问题标题】:iOS5 using ARC: Implicit conversion of int to NSDictionaryiOS5 使用 ARC:int 到 NSDictionary 的隐式转换
【发布时间】:2012-02-06 14:05:58
【问题描述】:

我正在使用 ARC 来更新我的旧项目。 我有一个“Filehelper”类,我在其中使用 c 函数,例如对于我在几乎每个项目中都需要的方法。 (eg.load plist, etc..)

视图控制器

NSDictionary* dictionary = getDictionaryFromPlistWithName(@"TestFile", @"plist");

文件助手.m

#pragma Returns content of plist as NSDictionary
NSDictionary* getDictionaryFromPlistWithName(NSString* fileName, NSString* pathExtension) {

    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:pathExtension];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];

    if(fileExists){
        NSDictionary* dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
        return dictionary;
    }
    else{
        [NSException raise:@"File not found" format:@"path %@", path];
        return nil;
    }
}

我收到此错误:

*错误:自动引用计数问题:ARC 不允许将“int”隐式转换为“NSDictionary

任何想法如何修复它以在 iOS 5 中使用它? 我读过一些我需要使用的东西(__bridge NSDictionary*),但这并没有帮助。

附言。 您已经需要的课程的工作流程是什么?也使用 C 函数?= 最好的方法是什么?

【问题讨论】:

  • 哪一行产生错误?
  • 为什么你使用 C 风格的函数 NSDictionary* getDictionaryFromPlistWithName(NSString* fileName, NSString* pathExtension) 声明而不是像 - (NSDictionary *) dictionaryFromPlistWithName:(NSString *) fileName extension:(NSString *) pathExtension 这样声明 Objective-C 方法?
  • @AndreyZ。因为也许他希望这是一个 C 函数,而不一定要附加到特定的类?
  • 是否还有其他警告,例如关于找不到函数定义?

标签: ios automatic-ref-counting


【解决方案1】:

这里最可能的问题是您忘记了#include 您的头文件。我敢打赌,您会忽略进一步的警告,尤其是说“getDictionaryFromPlistWithName 的未知签名,假设它返回 int”之类的警告。通读您的警告并且永远不要忽略它们(我强烈建议所有 ObjC 项目使用 -Werror)。

【讨论】:

  • 谢谢,罗伯。但是没有任何警告。我不会忽视他们。不过谢谢你的提示。
  • 但是现在可以了。也许只是我忘了像你说的那样包含标题。我的错;)谢谢大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多