【问题标题】:Incompatible pointer type warning using NSOpenPanel使用 NSOpenPanel 的不兼容指针类型警告
【发布时间】:2013-05-26 18:40:08
【问题描述】:

我有一些用于打开文本文件的代码,它运行良好,但现在它的某些部分已被弃用,因此我更改了它们以使其正常运行而不会出现任何错误。 我完成了一个工作正常的新代码,但它给了我一个警告,我无法理解如何修复它...... 这是我的代码:

-(IBAction)openMyFile:(id)sender
{
    int i;

    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSURL *myUrl = [NSURL fileURLWithPath:documentsDirectory]; //before here I had setDirectory but now is deprecated
    [openDlg setDirectoryURL:myUrl];

    [openDlg setCanChooseFiles:YES];

    [openDlg setCanChooseDirectories:YES];


    if ( [openDlg runModal] == NSOKButton )
    {

        NSArray* files = [openDlg URLs]; // here I had [openDlg filenames] but now is deprecated

        for( i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];

            NSString *content = [NSString stringWithContentsOfURL:fileName encoding:NSUTF8StringEncoding error:nil]; //HERE IS WHERE I GET THE WARNING
        }
     }
}

我收到的警告说:

不兼容的指针类型将“nsstring *__strong”发送到“nsurl *”类型的参数

当我尝试在 NSString *content 中传递文件的内容时它会出现,但无论如何内容都充满了文件的内容......一切都很好...... 任何帮助将不胜感激......和平 - 马西

【问题讨论】:

  • 您正在传递一个 NSString ,其中需要一个 NSURL ..
  • 你说得对...我只需要用 NSUrl 更改 NSString 就可以了...非常感谢!

标签: objective-c cocoa nsopenpanel


【解决方案1】:

[openDlg URLs] 返回一个 URLs 数组,而不是 字符串

NSArray *files = [openDlg URLs];
for(i = 0; i < [files count]; i++)
{
    NSURL *fileURL = [files objectAtIndex:i];
    NSString *content = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
}

【讨论】:

  • 感谢您的帮助... d4Rk 为我指明了正确的方向...但是当您编写解决方案作为答案时,我会将您的解决方案称为“绿色解决方案”。再次感谢...和平 - 马西
猜你喜欢
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多