Log ging custom objects and their properties

Please share your functions/code-snips

Log ging custom objects and their properties

Postby udo.killermann » Sat May 19, 2012 4:34 pm

Hello Board,

if you design a custom class, instantiate it and then want to Log the properties of its instance. You will come up with Log(theInstance). But instead of logging the properties this statement just logs the class name and memory location of the instance. In order to get what you want, you need to implement a description function that returns a string to represent the internal state.

OK - here comes the code (UKObject.NSObject)
Code: Select all
Declare Class "NSNumber"

Declare Function "NSNumber" - (NSString *)description
Declare Function "String" - (NSString *)description

Dim amount As NSNumber
Dim name As String

Function amount() As NSNumber
  Return amount
End Function

Sub setAmount(theAmount As NSNumber)
End Sub

Function name() As String
  Return name
End Function
Sub setName(theName As String)
End Sub

' this function needs to be defined if we want to log more than
' the name of the class
Function description() As String
  Return(Concat("{",amount.description(),", ",name.description(),"}"))
End Function     

The magic is done in the last lines.

If you now create an instance (e.g. inside Globals's AwakeFromNib):
Code: Select all
   Dim myObject1 As UKObject


  Log(myObject1) ' or Alert(myObject1)

the last line should log its state and not the generic information shown before.

Stay tuned
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Return to Code examples

Who is online

Users browsing this forum: No registered users and 1 guest