Page 1 of 1

Key Value Coding (KVC)

PostPosted: Tue May 22, 2012 7:12 pm
by udo.killermann
Bernd posted for the last edition of OBjB an example how to use Key Value Coding. KVC is a cornerstone of a Cocoa technique called binding. You can bind UI elements to an instance by qualifying the path to the values. Instead of Objective-C's "." (dot) Objective-Basic uses "!" (exclamation) to access the properties of an instance.

Here comes an example class that defines setters and getters - these can be seen as properties.

Code: Select all
' this is ukPerson.NSObject
Dim sName As String
Dim iAge As Integer

Sub setName(name As String)
  sName=name
  Log("setter called ")
End Sub

Function name() As String
    Log("getter called")
    Return(sName)
End Function

Function age() As Integer
  Return(iAge)
End Function

Function setAge(age As Integer)
  If(age > 17) Then
    iAge = age
 Else
    iAge = 18
  End If
End Function


Here is an example how to use this class:
Code: Select all
' this is Global
Dim thePerson As ukPerson

Event AwakeFromNib()
' assign values to the instance's properties
  thePerson!name = "Udo"
  thePerson!age = 16
 
' access values of instance's properties
  Alert(thePerson!name)
  Alert(thePerson!age)
End Event


The project:
kvcSetter.zip
(744.24 KiB) Downloaded 155 times


Two questions to my readers:
What do you code in Objective Basic?
Why aren't you coding in Objective Basic yet?

It will be interesting to read your answers.

Keep on coding
Udo