ヘッダー KTLog.h で関数を宣言します。
- (NSString *)logfileName;
そして、これを KTLog.m のように実装します。
- (NSString *)logfileName
{
NSString *logPath = [[NSString stringWithFormat:@"%@", NSHomeDirectory()] stringByAppendingPathComponent:@"Library/Logs/"];
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir;
if (!([fm fileExistsAtPath:logPath isDirectory:&isDir] && isDir))
{
NSURL *nsrulLogpath = [NSURL URLWithString:logPath];
if (![fm createDirectoryAtURL:nsrulLogpath withIntermediateDirectories:nil attributes:nil error:nil])
{
NSLog(@"Failed to create log directory: %@", logPath);
}
}
NSString *processName = [[NSProcessInfo processInfo] processName];
NSString *logName = [logPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.ktlog", processName]];
if (![fm fileExistsAtPath:logName])
{
if (![fm createFileAtPath:logName contents:[NSData data] attributes:nil])
{
NSLog(@"Failed to create log file at: %@", logName);
}
}
return logName;
}
しかし、この関数をこのように呼び出すと、
NSDictionary *logAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:[self logfileName] eror:&error];
エラーが発生しました。KTLog.m ファイルに KTLog.h をインポートします。しかし、私はこのエラーを理解できませんでした。私を助けてください。