2

I would like to be able to use NSLog with Swift structs. Can I? What I'm doing right now is:

extension CGRect {
    var description:String {
        return "\(self.left),\(self.top)_|\(self.width),\(self.height)"
    }
 }

NSLog("rect for something %@", self.someView.frame.description)

I had hoped that simply adding the description var to CGRect would be enough and I wouldn't need to explicitly add the .description in the NSLog() call. But when I try that I get

Argument type 'CGRect' does not conform to expected type 'CVarArgType'

It seems that arrays of structs have similar issues (because arrays are structs too?)

2
  • 1
    Looks like you have to make CGRect adopt the CVarArgType protocol then. Commented Sep 14, 2015 at 22:53
  • I found the doc for the CVarArgType protocol, but it's pretty short on details what one does to make a type adopt it. It does say that many value types adopt it, so I have to believe that CGRect could be added to that list. Commented Sep 14, 2015 at 23:27

2 Answers 2

9

As far as NSLog is concerned, CGRect is not a Swift struct. It's a C struct. And there is already a C function that lets you log one: NSStringFromCGRect. That is exactly what this function is for:

NSLog("rect for something %@", NSStringFromCGRect(self.someView.frame))

On the other hand, Swift does already give a CGRect its own string representation, and you can get it simply by asking for that String:

NSLog("rect for something %@", String(describing: self.someView.frame))

But you cannot use a CGRect directly in any kind of NSLog call. It isn't something that has a corresponding format specifier. If you want to use a CGRect with NSLog, you must pass it through some call that yields something NSLog can deal with. The fact that you are writing this code in Swift is irrelevant; to use NSLog, you must use it exactly as if this were Objective-C/C, because NSLog is Objective-C/C.

What isn't quite clear is why you're using NSLog here at all, since it doesn't sound like you have much idea what you're doing. And besides, print will do this for you and it will automatically call your description if you conform to Printable (now called CustomStringConvertible because Apple doesn't care how long and ugly the names of things are).

3
  • I seem to find lots of blogs and such that say that NSLog is both slower and safer (and provides things like timestamp info). This isn't in a workspace, so I guess I'd rather just keep using NSLog. Commented Sep 14, 2015 at 23:23
  • I'm not sure how one distinguishes between a C struct and Swift struct? I've added lots of useful extensions to various CG structs. That makes them seem pretty swifty (even if automagically converted) to me Commented Sep 14, 2015 at 23:24
  • You asked why you couldn't remove description and have NSLog work. I'm explaining it to you. You are not in the Swift world any more. You are using NSLog. So now you must look at the world from the perspective of C and Objective-C. And from their point of view, CGRect is not a "%@" object. It isn't an object of any kind! To them, it is a C struct. That is also why you can't make an NSArray of them; an NSArray can contain only objects - objects from Objective-C's point of view.
    – matt
    Commented Sep 15, 2015 at 1:13
0

You can use 'extension' for C struct in swift and you may write this in Xcode6(Swift1.2).

extension CGRect: Printable {
    public var description:String {
        return "\(self.minX),\(self.minY)_|\(self.width),\(self.height)"
    }
}

NSLog("rect for something %@", self.view.frame.description)

result:

2015-09-15 14:00:04.072 aaa[7330:158325] rect for something 0.0,0.0_|375.0,667.0
1
  • But there is no need for this, because CGRect already conforms to Printable (now called CustomStringConvertible).
    – matt
    Commented Jun 16, 2020 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.