How do I call setStatus from within awakeFromNib?
-(void)awakeFromNib {
setStatus; // how?
}
/* Function for setting window status */
- (void)setStatus {
[statusField setStringValue:#"Idle"];
}
In Objective-C, you use self to refer to the current object:
[self setStatus];
Maybe you might want to revise that method to be this:
- ( void ) setStatus: ( NSString *) status {
[ statusField setStringValue: status ];
}
You can then call it like this:
[ self setStatus: #"Idle" ];
Related
I'm working on a react-native project that requires some native modules. One of them is a Bluetooth module that allows me to access some CSRGaia methods. Ultimately, I want to be able to read the eq values on the PS-key so that I can set my equalizer to the corresponding values. I know almost nothing about Objective-C
Currently there is a method that looks like this:
RCT_EXPORT_METHOD(setEQValues:(NSArray *)values callback:(RCTResponseSenderBlock)callback)
{
CSRPeripheral *connectedPeripheral = [CSRConnectionManager sharedInstance].connectedPeripheral;
if( connectedPeripheral == nil )
{
callback(#[DISCONNECTED]);
return;
}
[[CSRGaia sharedInstance] setEQValues:values];
}
This works with no issues. However, when I tried to write my own
RCT_EXPORT_METHOD(getUserEQ: (NSArray *)values callback:(RCTResponseSenderBlock)callback)
{
CSRPeripheral *connectedPeripheral = [CSRConnectionManager sharedInstance].connectedPeripheral;
if( connectedPeripheral == nil)
{
callback(#[DISCONNECTED]);
return;
}
[[CSRGaia sharedInstance] getUserEQ: values];
}
I get the following error:
No visible #interface for 'CSRGaia' declares the selector 'getUserEQ:'
I double checked the CSRGaia.m file to verify that both methods exist.
- (void)setEQValues:(NSArray *)values {
NSMutableData *payload = [[NSMutableData alloc] init];
for( NSNumber *value in values ) {
uint8_t hex = [value unsignedCharValue];
[payload appendBytes:&hex length:1];
}
[self sendCommand:GaiaCommand_SET_HEP_EQ_PSKEY
vendor:CSR_GAIA_VENDOR_ID
data:payload];
}
- (void)getUserEQ {
[self sendCommand:GaiaCommand_GetUserEQControl
vendor:CSR_GAIA_VENDOR_ID
data:nil];
}
you are calling this method:
'getUserEQ:'
notice the 2 dots colon
it's different from method
'getUser'
with no colon
and in your .m file there is only
- (void)getUserEQ {}
i guess you wanted to use the setter method, instead
- (void)setEQValues:(NSArray *)values{}
like this:
[[CSRGaia sharedInstance] setEQValues: values];
add anyway both
- (void)getUserEQ;
- (void)setEQValues:(NSArray *)values;
in CSRGaia.h file
between
#interface OSRGaia
and
#end
In the MyViewController.h file:
#property (nonatomic, copy, nullable, class) void (^saveMetadataSuccess)(MyViewController*const _Nullable myViewController);
In the MyViewController.m file:
void (^saveMetadataSuccess)(MyViewControllerr* const myViewController) = nil;
+ (void)setSaveMetadataSuccess:(void (^)(MyViewController* const))newMetadataSaveSuccess {
saveMetadataSuccess = [newMetadataSaveSuccess copy];
}
+ (void (^)(MyViewController* const))saveMetadataSuccess {
return saveMetadataSuccess;
}
And finally the method which I don't understand:
- (void)success {
dispatch_async(dispatch_get_main_queue(), ^{
MyViewController.saveMetadataSuccess(self);
});
}
From my understanding, saveMetadataSuccess is a getter, but MyViewController.saveMetadataSuccess(self);seems to set something.
Can somebody enlighten me?
Thanks
MyViewController.saveMetadataSuccess is a getter and it returns a block that then being called with a param (self).
So it's like a function that returns other function.
Also you must not just call MyViewController.saveMetadataSuccess(self); because MyViewController.saveMetadataSuccess is nullable and it will crash if MyViewController.saveMetadataSuccess is null.
You have to check MyViewController.saveMetadataSuccess first:
- (void)success {
dispatch_async(dispatch_get_main_queue(), ^{
if (MyViewController.saveMetadataSuccess) {
MyViewController.saveMetadataSuccess(self);
}
});
}
for example:
-(void) myExample {
..do something
}
void myOther(){
how to call myExample function here
}
When you call myOther, pass self reference. you should define the C method like this:
void myOther(id callBack)
Now you have self reference in c function.
void myOther(id callBack){
[callBack myExample];
}
If both methods are in same Class than you can directly call First method from Second methods as follows:
-(void) myExample {
..do something
}
void myOther(){
call to myExample function
[self myExample];
}
read docs here: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html
void getInputSource() {
TISInputSourceRef source = TISCopyCurrentKeyboardLayoutInputSource();
NSLog(#"languages: %#", TISGetInputSourceProperty(source, kTISPropertyBundleID));
NSLog(#"localized name: %#", TISGetInputSourceProperty(source, kTISPropertyLocalizedName));
[self awakeFromNib];
}
-(void) awakeFromNib {
self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
NSImage* icon = [NSImage imageNamed:#"icon.png"];
self.statusBar.image = icon;
}
How would I go about passing parameters when calling a void method? I understand that you can do something like this:
-(void)viewDidLoad {
[self callMethod];
}
-(void)callMethod {
//stuff here
}
But how would I pass a parameter, such as an NSString, to the callMethod method?
Here is an example with an integer parameter.
-(void)viewDidLoad {
[self callMethodWithCount:10];
}
-(void)callMethodWithCount:(NSInteger)count {
//stuff here
}
In objective-c the parameters are included within the method name. You can add multiple parameters like this:
-(void)viewDidLoad {
[self callMethodWithCount:10 animated:YES];
}
-(void)callMethodWithCount:(NSInteger)count animated:(BOOL)animate{
//stuff here
}
It seems you may be misunderstanding what the void in the beginning of the method means. It's the return value. For a void method, nothing is returned from calling the method. If you wanted to return a value from your method you would do it like this:
-(void)viewDidLoad {
int myInt = [self callMethodWithCount:10 animated:YES];
}
-(int)callMethodWithCount:(NSInteger)count animated:(BOOL)animate{
return 10;
}
You define your method to return an int (in this example it always returns 10.) Then you can set an integer to the value returned by calling the method.
- (void)callMethod:(NSString *)string
{
}
Where string is your parameter so you would call
NSString *myString = #"your string here......";
[self callMethod:myString];
Suppose I have a parent init method like so:
- (id)initWithValue:(int)val name:(NSString *)n
{
value = val;
name = n;
}
and a child class with an init like so:
- (id)initWithAge:(int)a
{
age = a;
}
Is there a way to do the following:
[Child initWithAge:10 value:12 name:#"Sam"];
where the age will go to the child init method and the "value" and "name" go to the parent method? Or does every child have to expect all arguments it expects and pass it to the parent using [super init]??
- (id)initWithAge:(int)a value:(int)val name:(NSString *)n
{
self = [super initWithValue:val name:n];
if(self)
{
age = a;
}
return self;
}
In Child, you would have to declare initWithAge: value: name:. Nothing will "go to the parent" method explicitly without some work on your part.
You would declare amethod in Child:
-(id)initWithAge:(int)age value:(iint)value name:(NSString*)name {
if ( (self = [super initWithValue:value name:name]) ) {
_age = age; // assuming you an ivar '_age'
}
return self;
}
You can pass certain things on to the super class, but only what it is expecting, nothing will create methods for you.
Have read on inheritance and polymorphism, and it important to understand what self is.