0

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

-(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 中传递文件的内容时它会出现,但无论如何内容都充满了文件的内容......一切都很好......任何帮助将不胜感激...... . 和平 - 马西

4

1 回答 1

0

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

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];
}
于 2013-05-26T18:45:21.883 回答