problems with KVC and MutableArray

Coding Questions, IDE related or anything else

problems with KVC and MutableArray

Postby udo.killermann » Sun Jul 04, 2010 12:34 pm

Hello Bernd,

I am at the moment experimenting with KVC and Objective-Basic. It works very well with scalar types like NSString and company. But it doesn't work for arrays (at least not for me). The scalar version allows binding to UI elements within Interface Builder and allows greater flexibilty for me. It would be kind of you to look at the code and perhaps give a hint:
Code: Select all
' global
' declare section

Declare Function "NSMutableArray" - (NSMutableArray *)mutableArrayValueForKey:(NSString *)key
Declare Function "NSMutableArray" - (id)valueForKey:(NSString *)key
Declare Function "NSMutableArray" - (void)setValue:(id)value forKey:(NSString *)key
Declare Function "NSMutableArray" - (void)insertObject:(id)anObject atIndex:(NSUInteger)index

Declare Function "ukBaseClass" - (NSMutableArray *)mutableArrayValueForKey:(NSString *)key
Declare Function "ukBaseClass" - (id)valueForKey:(NSString *)key
Declare Sub "ukBaseClass" - (void)setValue:(id)value forKey:(NSString *)key
Declare Function "ukBaseClass" - (id)valueForUndefinedKey:(NSString *)key

' golbal variable section
Dim myValue As String
Dim myTitle As String
Dim bold As Boolean
Dim myArray As Array
Dim myMuArray As NSMutableArray
Dim myMArray As NSMutableArray
Dim myResArray As NSMutableArray
Dim myInstance As ukBaseClass

Function myValue() As String
  Return myValue
End Function

Sub setMyValue(theValue As String)
  Alert("New Value:",theValue)
End Sub

Function myTitle() As String
  Return myTitle
End Function

Sub setMyTitle(theValue As String)
  Alert("New Value:",theValue)
End Sub

Function bold() As Boolean
  Return bold
End Function

Sub setBold(theValue As Boolean)
  Alert("New Value:",theValue)
  bold = theValue
End Sub

Event Init()
  myValue = String("hund")
  myTitle = String("alles wird gut!")
  bold = Yes
  myArray = Array("bär", "katze", "maus")
  myMArray = myArray
  Alert("next: mutableArrayForKey")
  myMuArray = myInstance.valueForKey("myArray")
  ' myMuArray.insertObject("hund",atIndex:=0) 
  Alert("Next: valueForKey")
  myMuArray.setValue(myArray, forKey:="myArray")
End Event

Code: Select all
' ukBaseClass
Declare Class "NSUInteger"

Private Dim myArray As NSMutableArray
Public Dim hund As String

Function myArray() As NSMutableArray
  Alert("get my array",myArray)
  Return myArray
End Function

Sub setMyArray(theArray As NSMutableArray)
  Alert("set my array")
End Sub

Function hund() As String
End Function
Sub setHund(derHund As String)
  Alert("setz den Hund:",derHund)
End Sub

'Function countOfMyArray() As NSUInteger

'End Function
Function valueForUndefinedKey(key As NSString) As id
End Function

Event Init()
  ' myArray = Array("Bär", "Katze", "Maus")
  Alert("Class of myArray:",ClassName(myArray))
End Event

The application crashes with the following screen:

kvcArray.jpg (123.08 KiB) Viewed 805 times

The error gives a hint that the runtime interprets the values within the array as keys, which is not the way I itended the things to work. Is it that the resulting code doesn't attach the NSMutableArray class to myArray at all? The alert tells me that the class of myArray is NSCFArray which is a Core Foundation and not a Cocoa class.

Kind regards,
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

Re: problems with KVC and MutableArray

Postby berndnoetscher » Tue Jul 06, 2010 8:03 pm

Udo, sorry for the delay. Will find a solution soon...
Site Admin
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Re: problems with KVC and MutableArray

Postby berndnoetscher » Thu Jul 08, 2010 10:45 am

Hi Udo,

the line
Code: Select all
myMuArray.setValue(myArray, forKey:="myArray")

must become
Code: Select all
myInstance.setValue(myArray, forKey:="myArray")

By the way, it is possible to use the ! operator

Code: Select all
myInstance!myArray = myArray
Site Admin
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Return to Objective-Basic (Xcode 3)

Who is online

Users browsing this forum: No registered users and 1 guest