calling with selector

Coding Questions, IDE related or anything else

calling with selector

Postby udo.killermann » Mon Apr 26, 2010 5:50 am

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 ;)

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: calling with selector

Postby berndnoetscher » Mon Apr 26, 2010 9:49 am

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.
Site Admin
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Re: calling with selector

Postby udo.killermann » Tue Apr 27, 2010 5:54 am

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,
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 Objective-Basic (Xcode 3)

Who is online

Users browsing this forum: No registered users and 0 guests