Declare for uppercaseString

Because of a bug in Xojo uppercase (<https://xojo.com/issue/28024>) I’d like to do this via a Declare. I’ve found uppercaseString (Cocoa, in NSString.h), that should work, but I’m having a hard time getting the declare right – I’m not sure how to pass a pointer to the string (perhaps a memory block? – but I suspect there’s something easier). Any clues?

I haven’t looked. Was this implemented in MacOSLib?

There is a CoreFoundation method to uppercase, but it’s not used in the examples and to be honest, since it’s not a callable method, I couldn’t see how to use it.

Hi Jonathan,
You should check out the short tutorial I wrote on how to do declares in this thread https://forum.xojo.com/5179-mac-os-custom-fonts/ , once you get a grasp of how to declare, it opens up a whole new world to you.

Start by looking at the NSString function “uppercaseString”.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/uppercaseString

[code]uppercaseString
Returns a uppercased representation of the receiver.

  • (NSString *)uppercaseString[/code]

If you’ve read the short tutorial, you’ll notice that this function is different from the ones used in the tutorial, reason being, this is an Objective function, not a flat function (i.e. it just contains the function name, and is not proceeded by a class name).

The “-” indicates that this is a instance method (just like the ‘method’ of a Xojo object). If there was a “+” it’s a class method or shared method in Xojo terms.

(NSString *) is the return type of the function, we’re going to cheat here and us NSCFbridging rather than creating a NSString, we’ll use the Xojo CFStringRef object.

But how do you know what to pass in? For this function, there are no parameters (by the absence of “:” in the function name), but you still need to tell Cocoa which Object to call this method on. So in Xojo we must pass it an object, and in this case we’ll once again cheat and use a CFStringRef. We must use a “selector” to tell Cocoa that we’re calling an Objective method not trying to call a flat method.

If you create a new project and add a Label to the default window, add the following code to the open event, then run it.

Declare function uppercaseString lib "Cocoa" selector "uppercaseString" ( inString as CFStringRef ) as CFStringRef label1.text = upperCaseString( "This is a test string" )

Hopefully the code should make some sense and not only solve your problem, but also send you on your way into the Cocoa toolbox.

Wow, what a great reply. Thank you very much!

You’re welcome - plus it was an easy declare to do, it has been more complicated I may not have created such a response :slight_smile: