swiftswiftlint

How do you split up OSLogMessage parameters to avoid a line length violation with SwiftLint?


I just installed SwiftLint and integrated it with Xcode. I'm trying to resolve a line length violation for the following line:

Log.persistence.debug("Initialized \(self.instances.value.count) instances of type \(CoreDataEntity.self, privacy: .public) from CoreData.")

SwiftLint shows:

Line Length Violation: Line should be 120 characters or less: currently 152 characters (line_length)

I tried to resolve this by splitting up the message string using the +operator, but that did not work:

Log.persistence.error("Instances of type \(CoreDataEntity.self, privacy: .public) "
                      + "could not be initialized from CoreData.")

results in a compiler error:

Cannot convert value of type 'String' to expected argument type 'OSLogMessage'

Of course I could just customize the SwiftLint rule to ignore interpolated strings (which would work), but I'd prefer to somehow split up the messages instead. Any ideas about this?


Solution

  • You could use a multiline string literal. Break the string literal up into multiple lines and put a backslash character (\) at the end of a line when you don't want to include the line break in the string, like this:

    Log.persistence.debug(
        """
        Initialized \(self.instances.value.count) instances \
        of type \(CoreDataEntity.self, privacy: .public) \
        from CoreData.
        """
    )