【问题标题】:Storing file path in core data throws exception在核心数据中存储文件路径会引发异常
【发布时间】:2013-03-08 11:22:33
【问题描述】:

我正在尝试将设备文档文件夹中的文件的文件路径存储在核心数据实体中,它可以正确插入,但是当我尝试使用路径属性上的谓词获取记录时,它会抛出异常.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the
 format string "filePath==/Users/****/Library/Application Support/iPhone Simulator/5.1/Applications
/8C1B07FD-E372-4CD8-9A02-FDA321ECE629/Documents"'

属性已正确存储在 Core Data DB 中。

要提取的代码。

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"filePath==%@",Path]];
    NSSortDescriptor *sortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"fileName" ascending:YES];
    fetchRequest.sortDescriptors=[NSArray arrayWithObject:sortDescriptor];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"File"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSArray *fetchedObjects = [self.managedObjectContext
                               executeFetchRequest:fetchRequest error:nil
                               ];

【问题讨论】:

    标签: ios ios5 core-data ios6


    【解决方案1】:

    可能文件路径包含妨碍正确解析的字符,例如空格。试试这个:

    [NSPredicate predicateWithFormat:@"filePath = '%@'", path]; 
    

    【讨论】:

      【解决方案2】:

      NSPredicate 类参考中的示例对字符串使用双引号,例如

      Simple comparisons, such as grade == "7" or firstName like "Shaffiq"
      

      显示的错误包含一个不带引号的谓词 - 可能是问题的根源?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 2018-12-26
        • 1970-01-01
        • 2011-05-30
        • 1970-01-01
        相关资源
        最近更新 更多