iOS5使用ARC:int到NSDictionary的隐式转换

发布于 2025-01-02 11:52:59 字数 1074 浏览 2 评论 0原文

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

ViewController

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

Filehelper.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 函数吗?= 最好的方法是什么?

i´m using ARC to update my old project.
I have a "Filehelper" class in which i use c-functions e.g. for methods i need in almost every projects. (eg.load plist, etc..)

ViewController

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

Filehelper.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;
    }
}

I get this error:

*error: Automatic Reference Counting Issue: Implicit conversion of 'int' to 'NSDictionary ' is disallowed with ARC

Any ideas how to fix it to use it with iOS 5?
I´ve read something that i need to use (__bridge NSDictionary*), but that didn´t help.

PS.
What´s your workflow for classes which you already need? Use C-functions, too?=
What´s the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-01-09 11:52:59

这里最可能的问题是您忘记#include您的头文件。我敢打赌,还有更多警告您会忽略,特别是一条警告,其中写着“getDictionaryFromPlistWithName 的未知签名,假设它返回 int”之类的内容。仔细阅读您的警告,切勿忽略它们(我强烈建议所有 ObjC 项目使用 -Werror)。

The most likely problem here is that you forgot to #include your header file. I bet there are further warnings that you're ignoring, particularly one that says something like "unknown signature for getDictionaryFromPlistWithName, assuming it returns int". Read through your warnings and never ignore them (I strongly recommend -Werror for all ObjC projects).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文