本文介绍了无法创建日志目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在iPad上测试我的应用程序时,它运行完美,即调用数据库,创建文件夹和执行其他任务,但在控制台它显示我

When I test my application on iPad it run perfectly i.e. call the database , create folder and perform other task but in console it show me

当我搜索时,找到此答案

When I search, found this answer NSFileManager creating folder (Cocoa error 513.)

但不能abel删除此。 $ b当我关机并重新启动iPad时,此行不显示

But not abel to remove this.
Also when I shutdown and restart the iPad this line is not show

问题:


  1. 为什么我在控制台中得到这个语句?

  2. 上述语句可能会在将来崩溃我的应用程式?

  3. 如何清除Cocoa错误513?

这是我调用数据库的代码

Here is my code to calling the database

let fileManager = NSFileManager()
var Sourcepath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("DataBase.db");
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
let databaseStr = "DataBase.db"
let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
println(dbPath)
if(fileManager .fileExistsAtPath(dbPath) == false) {

    var error:NSError?
    fileManager.copyItemAtPath(Sourcepath!, toPath: dbPath, error: &error)
    println(error)
}

这里是一个创建日志文件夹的函数

and here is a functions which create a logs folder

func CreateLog(Log:String)
{
    autoreleasepool{

        var formatter:NSDateFormatter! = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd";
        var DateString = formatter.stringFromDate(NSDate()).stringByAppendingString("_tempLogs")
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss";
        var FileManager:NSFileManager! = NSFileManager.defaultManager()

        var LogFolder = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("Logs")
        let searchPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        let LogStr = "Logs"
        let LogFolderPath = searchPath.stringByAppendingPathComponent(LogStr)

        if(FileManager.fileExistsAtPath(LogFolderPath) == false)
        {
            var error:NSError?
            FileManager.createDirectoryAtPath(LogFolderPath, withIntermediateDirectories: true, attributes: nil, error: &error)

        }
        var LogPath = LogFolderPath.stringByAppendingPathComponent(DateString).stringByAppendingPathExtension("txt")
        var WriteString = (formatter.stringFromDate(NSDate()).stringByAppendingString(" ").stringByAppendingString(Log).stringByAppendingString("\n"));
        var Data = WriteString.dataUsingEncoding(NSUTF8StringEncoding)
        if(!FileManager.fileExistsAtPath(LogPath!))
        {
            FileManager.createFileAtPath(LogPath!, contents: Data, attributes: nil)
        }
        else
        {
            var output = NSFileHandle(forWritingAtPath: LogPath!);
            output?.seekToEndOfFile();
            output?.writeData(Data!);
            output?.closeFile()
            output = nil
        }
        formatter = nil
        FileManager = nil

    }

}


推荐答案

string是yyyy-MM-dd HH:mm:ss,它在结果字符串中引入了一个空格。删除格式化程序中的空格,使其看起来像yyyy-MM-ddHH:mm:ss,然后尝试上面的代码,或者替换结果路径中的空格,并将数据写入文件

The format you using for getting date string is "yyyy-MM-dd HH:mm:ss" which introduces a space in the result string. Remove the space in the formatter so that it looks like this "yyyy-MM-ddHH:mm:ss" and try the above code or you could replace the space in the resulting path and write data to the file

这篇关于无法创建日志目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:29