Key Value Coding (KVC)

Please share your functions/code-snips

Key Value Coding (KVC)

Postby udo.killermann » Tue May 22, 2012 7:12 pm

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)
  Log("setter called ")
End Sub

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

Function age() As Integer
End Function

Function setAge(age As Integer)
  If(age > 17) Then
    iAge = age
    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
End Event

The project:
(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
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