[solved] (OSX) get/set cocoa property

Just starting out? Need help? Post your questions and find answers here.
erion
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Jan 24, 2010 11:12 pm

[solved] (OSX) get/set cocoa property

Post by erion »

Hello,

I am trying to retrieve/set various properties for classes, e.g. 'speaking' of NSSpeechSynthesizer.
As far as I know, CocoaMessage() allows methods to be called, but it does not support properties. Is there a way to do this in PB?

Erion
Last edited by erion on Thu Sep 17, 2015 8:46 pm, edited 1 time in total.
To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

- W. B.

Visit my site, also for PureBasic goodies http://erion.tdrealms.com
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: (OSX) get/set cocoa property

Post by wilbert »

erion wrote:I am trying to retrieve/set various properties for classes, e.g. 'speaking' of NSSpeechSynthesizer.
As far as I know, CocoaMessage() allows methods to be called, but it does not support properties. Is there a way to do this in PB?
A property is a combination of a getter and setter function or in case of readonly a getter function only.
If you look at the documentation for NSSpeechSynthesizer, it says
@property(getter=isSpeaking, readonly) BOOL speaking
So in this case the function you need is called isSpeaking

Code: Select all

SpeechSynthesizer = CocoaMessage(0, CocoaMessage(0, 0, "NSSpeechSynthesizer alloc"), "initWithVoice:$", @"com.apple.speech.synthesis.voice.Alex")

Debug CocoaMessage(0, SpeechSynthesizer, "isSpeaking")

CocoaMessage(0, SpeechSynthesizer, "startSpeakingString:$", @"Your Mac is speaking to you")

Delay(100)

Debug CocoaMessage(0, SpeechSynthesizer, "isSpeaking")

MessageRequester("", "Your Mac is speaking to you")
Windows (x64)
Raspberry Pi OS (Arm64)
erion
Enthusiast
Enthusiast
Posts: 128
Joined: Sun Jan 24, 2010 11:12 pm

Re: (OSX) get/set cocoa property

Post by erion »

Marvellous, thank you!
I wasn't expecting getter/setter exposure because of my Windows background, but this definitely makes things easier.
Now I wish we had native Cocoa type conversion, such as NSArray to PB array, etc. :)

Once again, huge thanks for your help!
To see a world in a grain of sand,
And a heaven in a wild flower,
Hold infinity in the palm of your hand,
And eternity in an hour.

- W. B.

Visit my site, also for PureBasic goodies http://erion.tdrealms.com
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: (OSX) get/set cocoa property

Post by wilbert »

erion wrote:Now I wish we had native Cocoa type conversion, such as NSArray to PB array, etc. :)
That would be very difficult.
To convert a PB string array for example, you would need to convert each item into a NSString also.
But when you convert an array of integer values, you would need a different approach.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply