How to call - (void) function in ordinary void function in cocoa - objective-c

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

Related

OCMVerify crashes test after a mocked callback

I have a method from a class under tests which takes two delegates: the second delegate will call a method after the first delegate will be called with a callback function as input.
#implemenation ClassUnderTest
...
- (void) methodWithMultipleCallbacks: (id<MyDelegate>) delegate
withSecondDelegate: (id<MyDelegateWithCallback>) delegateWithCallback {
for (int i = 0; i < 2; i++){
Callback callback = ^(int input) {
NSLog(#"%d-th callback", i);
NSLog(#"input = %d", input);
};
[delegateWithCallback fetchInt:callback];
}
[delegate delegateDoStuff:32];
}
But the strange thing happens: I tried to test it using OCMock, mocking both delegates, but the crashes, and I got a EXEC_BAD_ACCESS.
I am utterly confused and would really appreciate any help here! Here's the test function
- (void) testWithMultipleCallbacks {
id <MyDelegateWithCallback> mockDelegateWithCallback = OCMProtocolMock(#protocol(MyDelegateWithCallback));
OCMStub([mockDelegateWithCallback fetchInt:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
void (^block)(int) = NULL;
[invocation getArgument:&block atIndex:2];
NSLog(#"got here");
block(33);
});
[_classUnderTest methodWithMultipleCallbacks: _mockedDelegate withSecondDelegate: mockDelegateWithCallback];
OCMVerify(OCMTimes(1), [_mockedDelegate delegateDoStuff:[OCMArg any]]);
}

Using method_exchangeImplementations with block_invoke methods

I'm trying to replace method implementation with the following code
BOOL ct_hookMethod(Class originalClass, SEL originalSelector, Class swizzledClass, SEL swizzledSelector)
{
Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(swizzledClass, swizzledSelector);
if (originalMethod && swizzledMethod)
{
method_exchangeImplementations(originalMethod, swizzledMethod);
return YES;
}
return NO;
}
It works for selectors like -[xxxView runRequest:], but not for methods like -[xxView runRequest:]_block_invoke:.
Does anyone know how to hook them?

Objective-C: What does this code mean?

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

Objective-C passing parameters in void method

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

How to call method inside class

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" ];