Page 1 of 1

calling with selector

PostPosted: Mon Apr 26, 2010 5:50 am
by udo.killermann
Hi Board,

yesterday I played arround with selectors an was quite satisfied with the results. Suddenly I stumbled because of arguments or return values that were more or less atomic types and obviously not classes. Perhaps it gets clearer, if you see my code:
Code: Select all
Declare Function "NSString" + (id)stringWithString:(NSString *)aString
Declare Function "NSString" - (BOOL)hasSuffix:(NSString *)aString
' - (NSString *)stringByAppendingString:(NSString *)aString

Declare Class "NSNumber"
Declare Function "NSNumber" + (NSNumber *)numberWithInteger:(NSInteger)value

' to capture the Log messages in a file
' invoke the app from commandline and redirect stderr to file via <appname> 2> err.out

Dim myString As NSString

Event AwakeFromNib()
  Dim myLength As Integer
  Dim myNumber As NSNumber
  Dim myBool As Boolean
  Dim mySuffix As String
  Dim myObj As id
  myString = NSString.stringWithString("hund katze maus")
  mySuffix = NSString.stringWithString(" aus")
  myNumber = NSNumber.numberWithInteger(1)

  ' this construct should help to capture the return value, because it is
  ' declared to be of id type
  ' but NSString's length returns a (NSUInteger) which is basically a
  ' (unsigned int)

  ' but this doesn't work
  myObj = myString.PerformSelector0("length")
  ' check to see, if NSString instance answers to hasSuffix message 
  Log(myString.RespondsToSelector("hasSuffix:")) ' it responds!
  ' called directly it returns the correct result
'  myObj = myString.PerformSelector1("hasSuffix:", mySuffix) ' this doesn't crash, but doesn't produce results

'  Log(ClassName(myObj))
'  Log(myObj)
  ' this crashes directly - return value is (BOOL), another atomic/non class type
  Log(myString.PerformSelector1("hasSuffix:", mySuffix))
  Log(myString.PerformSelector1("stringByAppendingString:",mySuffix)) ' it works for NSString as result, but not other types

End Event

Perhaps this is a general problem, but perhaps I (again) miss some common knowledge ;)


Re: calling with selector

PostPosted: Mon Apr 26, 2010 9:49 am
by berndnoetscher
Hi Udo,

nice example. :-)

The point is that Cocoa does allow PerformSelector with id types only, which means all arguments and the return value must be of type id. The Cocoa documenation states that if you need other types, you can use NSInvocation, but this is not supported by ObjB yet. I added this missing feature to the todo list, but it is not that important for me. Sorry.

Re: calling with selector

PostPosted: Tue Apr 27, 2010 5:54 am
by udo.killermann
Hi Bernd,

tanks for your answer. OK, if relevant, I will with NSInvocation. For the time beeing I now know that this isn't possible out of the Cocoa box.
If I post code to the forum and it could serve as an example its free for you to take. I use these code snippets to gain insights into the underlying mechanisms and hopen others can profit from it as well.

Kind regards,