objective-c - 对文件的一些操作

标签 objective-c

我写了这个程序但无法得到输出,每次运行这个程序时我都得到唯一的第一个 NSlog 语句。我已经在此路径和内容中创建了我的文件。

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        NSString *fName = @"/Users/sahil/Documents/newfile/commands";
        NSFileManager * fm;
        NSDictionary *attr;
        fm=[NSFileManager defaultManager];

        if ([fm fileExistsAtPath: fName]== NO)
        {
            NSLog(@"file not exist");
            return 1;
        }
        if ([fm copyItemAtPath:fName toPath:@"newfile1" error:NULL] == NULL)
        {
            NSLog(@"file copy failed");
            return 2;
        }
        if ([fm contentsEqualAtPath:fName andPath:@"newfile1"]==NO)
        {
            NSLog(@"files are not =");
            return 3;
        }
        if ([fm moveItemAtPath:@"newfile1" toPath:@"newfile2" error:NULL]== NULL)
        {
            NSLog(@"file rename fail");
            return 4;
        }
        if ((attr=[fm attributesOfItemAtPath:@"newfile2" error:NULL])==nil)
        {
            NSLog(@"couldnt get file attributes");
            return 5;
        }
        NSLog(@"file size is %llu bytes",[[attr objectForKey:NSFileSize] unsignedLongLongValue]);
        if ([fm removeItemAtPath:fName error:NULL] == NULL)
        {
            NSLog(@"file removal failed");
            return 6;
        }
        NSLog(@"all ops were successful");
        NSLog(@"%@",[NSString stringWithContentsOfFile:@"newfile2" encoding:NSUTF8StringEncoding error:NULL]);
    }
    return 0;
}

请帮我解决这个问题。 谢谢

最佳答案

操作码:

if ([fm fileExistsAtPath: fName]== NO)
{
    NSLog(@"file not exist");
    return 1;
}

如果 NSLog 打印,将返回,因此程序将不再执行。

删除 return 1; 语句。

这假设“我得到了唯一的第一个 NSlog 语句。”表示语句:NSLog(@"file not exist"); 执行。

操作码:

if ([fm copyItemAtPath:fName toPath:@"newfile1" error:NULL] == NULL)
{
    NSLog(@"file copy failed");
    return 2;
}

添加错误日志:

NSError *error;
if ([fm copyItemAtPath:fName toPath:@"newfile1" error:&error] == NULL)
{
    NSLog(@"file copy failed, error: %@", error);
    return 2;
}

关于objective-c - 对文件的一些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21853926/

相关文章:

iOS:使用自定义形状模糊

ios - 如何保持 NSTimer 始终均匀工作

iphone - iCloud 不支持有序集..替代解决方案?

ios - iPhone 识别按下主页按钮

iphone - 动态检查类是否通过 respondsToSelector : 响应选择器

objective-c - 在事件应用程序上执行 KeyEquivalent

iphone - 每个 TableView Cell 都有一个图像,有些是 `hidden` ,我如何测试它们是否都没有隐藏?

ios - SKStreetType - 哪些类型实际上是高速公路?

ios - 自定义移动tableview单元格: How to always keep first cell at top of UITableView when "moveRowAtIndexPath"

ios - 重复的接口(interface)定义和预期的选择器错误