import Foundation
import os.log
extension OSLog {
private static var subsystem = Bundle.main.bundleIdentifier!
static let session = OSLog(subsystem: subsystem, category: "session")
static func Debug(_ s: StaticString, args: CVarArg...) {
os_log(s, log: OSLog.session, type: .debug, args)
}
static func Error(_ s: StaticString, args: CVarArg...) {
os_log(s, log: OSLog.session, type: .error, args)
}
static func Info(_ s: StaticString, args: CVarArg...) {
os_log(s, log: OSLog.session, type: .info, args)
}
}
I defined this extension for using OSLog instead of NSLog in my project. I then wrote the following code which uses the extension:
/// Instantiate an array of `Tweet`s from a property list.
///
/// - Parameter url: The URL for a property list made up of one or more `Tweet`s.
/// - Returns: Optinal `Tweet` array. If the property list exists, is not malformed,
/// and contains all of the key/value pairs required to create one or
/// more `Tweet` items, an array of said items will be returned. Othrwise, nil
func tweetsWithContentsOfURL(_ url: URL) -> [Tweet] {
do {
let data = try Data(contentsOf: url)
let decoder = PropertyListDecoder()
let tweets = try decoder.decode([Tweet].self, from: data)
return tweets
} catch {
OSLog.Error("Failed to load Tweet values from %@ with error: %@", args: String(describing: url), String(describing: error))
return []
}
}
Here are the arguments being passed into `OSLog.Error`:
(lldb) po args
▿ 2 elements
- 0 : "file:///var/mobile/Containers/Data/Application/AD339CF3-71DD-4CC3-876B-9668F51ECF2F/Documents/ClientTweets"
- 1 : "Error Domain=NSCocoaErrorDomain Code=260 \"The file “ClientTweets” couldn’t be opened because there is no such file.\" UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/AD339CF3-71DD-4CC3-876B-9668F51ECF2F/Documents/ClientTweets, NSUnderlyingError=0x2833b0b70 {Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"}}"
(lldb) po s
"Failed to load Tweet values from %@ with error: %@"
If the file I'm trying to load is empty, an error is thrown and an error is logged. I'm expecting this under certain test conditions. This code works just fine when running on the simulator. However, when runing on a device, invoking any of the status functions (Debug, Error, or Info) results in "Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)". I check at runtime in the debugger to make sure that `subsystem`, `sessions` and my arguments are all properly initialized and make sense. They look good so I do not understand why this is crashing.
What am I missing here?
-Michael