IOS/Objective-C: Shared Instance Method with argument - ios

Hi I have a class that I am lazyloading as a sharedInstance. I would like to be able to call a method in the class that requries an input variable but the following syntax is not working. It says there is no such property but I copied the property from the interface.
Here is code that is not working
int myPoints = 200;
//myStatusLevel is a method in ManagePoints that takes mypoints as an input
// its signature is (int)myStatusLevel: (int) points;
//the following line gives error prperty myStatusLevel not found
int myStatusLevel =[ManagePoints sharedInstance].myStatusLevel:myPoints;
Can this be done and, if so, what is the proper syntax? Thanks in advance for any suggestions.

If there is a method in ManagePoints with the following signature:
-(int)myStatusLevel:(int)points;
then you should be able call that function and pass an argument.
You need to use square brackets:
int myStatusLevel = [[ManagePoints sharedInstance] myStatusLevel:myPoints];

Related

Null aware function invocation operator

In the same way we can have
nullableClassInstance?.method(blah)
Is there a way to do
nullableFunctionInstance?(blah)
In other words, is there an operator that checks whether a function instance is not null, if so, invoke the function all in one line?
Using the call method, you can achieve what you want with:
nullableFunctionInstance?.call(blah)
There's also the apply method if you want to pass arguments.
If you have a Function Object , you can use the call method and send all the parameters to that which works exactly as calling the function. Here , you can use the null aware member access operator.
void myFun(int a , int b){...}
var myVar = myFun ;
call
The function myVar will only get called if its not null as shown below.
myVar?.call( arg1 , arg2 );
apply
If your function is dynamic or you wish to control which function is being called at run time , you can use the apply static method of Function like so :
Function.apply(myVar , [arg1 , arg2]);
apply takes the function and a List of parameters that will be sent to the function.
Read more about call and apply :

Method signature in Objective-C

I'm trying to understand, how does method signature in Objective-C is look like.
INTRO:
At first, lets break misunderstandings about question, what is it, method signature?
Method signature it is something, that helps compiler unambiguously identifies subroutine.
Am i right? :)
So in the C language signature is roughly equivalent to its prototype definition:
For example, we have function int printf( const char *format, ... ); in stdlib.
Signature of this function is printf.
In this case, we can't overload in C language, because compiler can't identify function with different argument types, so people decide to do some tricky thing like this:
long int labs (long int n);
int abs (int n);
double fabs (double x);
In the C++ language, method signature is class name, method name and method arguments.
So in this language we can overload methods.
PROBLEM
I can't get simple answer, what is method signature in Objective-C?!
I'm trying to use my logic...
1)At first, we can't overloading in Objective-C => method arguments is not part of method signature.
2)I tried to compile code with different return value:
#interface Foo : NSObject
- (CGFloat)method;
- (NSInteger)method;
#end
I got error in this case => return value is not part of method signature.
I tested different cases and got the answer, method signature in Objective-C is class name, method type ('+' or '-') and selector.
For example, we have method in class Foo (code below):
#interface Foo : NSObject
+ (void)methodWithArgument:(NSInteger)argument;
#end
So the signature of this method is +[Foo methodWithArgument:]
But then, i look at apple's documentation of NSMethodSignature (http://bit.ly/1tGR8zt)
An NSMethodSignature object records type information for the arguments
and return value of a method
Arguments and return value?! Only using arguments and return value, we can unambiguously identify method? It's very strange.
First thing. Who said that compiler can differentiate between 2 methods on the basis or its return type
so
#interface Foo : NSObject
- (CGFloat)method;
- (NSInteger)method;
#end
is wrong. Even in C or C++ you cannot overload method on the basis of its return type. Overloading can be performed on the basis of type of argument or number of arguments for a methods or both.
so over loading can be performed in this way only
#interface Foo : NSObject
-(ReturnType)methodNameHere:(int)argument;
-(ReturnType)methodNameHere:(int)argument secondArgumentDescription:(BOOL)anotherArgument;
-(ReturnType)methodNameHere;
compiler identifies different methods on the basis for number of arguments and their data type
Method signature is for developers and for runtime,
Not for compiler. (Compiler uses another technique to understand signatures)
you cannot identify methods that have the same name but different return types. If you call it while discarding the return value, which one should be called?
#interface Foo : NSObject
- (CGFloat)method;
- (NSInteger)method;
#end
Foo *foo = [Foo new];
[foo method]; // no use for return value here. Which implementation to call?

Getting the parameters of a method call from a clang match callback

I'm adapting the Clang tool-template (as described here) to search for a particular method call in my code. In order to later rewrite that call, I would like to get the type of the parameters the method was called with, as well as the type of the object the method was called on.
I managed to find a matcher that calls back the following:
class AddListenerPrinter : public MatchFinder::MatchCallback
{
public :
virtual void run(const MatchFinder::MatchResult &Result) {
if (const auto *FS = Result.Nodes.getNodeAs<clang::MemberExpr>("ListeningBound"))
{
FS->dump();
}
}
};
which prints out:
MemberExpr 0x7fb05b07b948 '<bound member function type>' .addListener 0x7fb05b077670
`-MemberExpr 0x7fb05b07b918 'class MyCore' lvalue ->mCore 0x7fb05b078e30
`-CXXThisExpr 0x7fb05b07b900 'class MyComponent *' this
Now I can't find any way to retrieve the type of the object the method was called on (here class MyCore) or the type of the method argument (here class MyComponent).
How can I do this?
I found the answer by browsing the code of the existing matchers.
Using matcher = memberCallExpr( callee(methodDecl(hasName("addListener"))) )
I was able to retrieve a CXXMemberCallExpr node. Then getting the type of the object the method was called on:
// FS is the CXXMemberCallExpr
// Prints out the type of x in x.method()
llvm::outs() << FS->getRecordDecl()->getName();
and the method parameters are accessible through FS->getArg(n).
Bottom line is: Find the CXX object that contains what you're looking for first (e.g. which class has methods to access function arguments?), then find the matcher that will return the same type of object in ASTMatchers.h.
Hoping this can help anybody else with the same problem.

Using mirrors, how can I get a reference to a class's method?

Say I have an instance of a class Foo, and I want to grab a list of all of its methods that are annotated a certain way. I want to have a reference to the method itself, so I'm not looking to use reflection to invoke the method each time, just to grab a reference to it the first time.
In other words, I want to do the reflection equivalent of this:
class Foo {
a() {print("a");}
}
void main() {
var f = new Foo();
var x = f.a; // Need reflective way of doing this
x(); // prints "a"
}
I have tried using InstanceMirror#getField, but methods are not considered fields so that didn't work. Any ideas?
As far as I understand reflection in Dart, there's no way to get the actual method as you wish to. (I'll very happily delete this answer if someone comes along and shows how to do that.)
The best I can come up with to ameliorate some of what you probably don't like about using reflection to invoke the method is this:
import 'dart:mirrors';
class Foo {
a() {print("a");}
}
void main() {
var f = new Foo();
final fMirror = reflect(f);
final aSym = new Symbol('a');
final x = () => fMirror.invoke(aSym, []);
x(); // prints "a"
}
Again, I know that's not quite what you're looking for, but I believe it's as close as you can get.
Side note: getField invokes the getter and returns the result -- it's actually fine if the getter is implemented as a method. It doesn't work for you here, but for a different reason than you thought.
What you're trying to get would be described as the "closurized" version of the method. That is, you want to get the method as a function, where the receiver is implicit in the function invocation. There isn't a way to get that from the mirror. You could get a methodMirror as
reflect(foo).type.methods[const Symbol("a")]
but you can't invoke the result.

Metatrader MQL4: Can't define function default values in .mqh file

I can't understand how to define default values for functions in my library. Default values tend to be ignored and I get "wrong parameters count" error message.
Here is my example. I created simple test library experts\libraries\test.mq4:
void test(int i = 0) // Note the default value for "i"
{
}
Then I created .mqh file as experts\include\test.mqh:
#import "test.ex4"
void test(int i = 0); // Note the default value for "i"
#import
Now I create simple expert "experts\simpletest.mq4":
#include <test.mqh>
int start()
{
// Should be able to call test() function without providing any arguments,
// because it has default value.
// If I change this line to test(0), everything compiles correctly
test(); // Causes "wrong parameters count" compilation error
return(0);
}
And I get the following error for test() function call:
')' - wrong parameters count
If I change this function call to test(0), everything compiles, but I should be able to call test() function without providing any parameters, because I have default value for first parameter in .mqh file, like this: void test(int i = 0);
Why it doesn't use the default value?
I search google for any clue, but can't find any references about this problem. Anybody knows?
This is not possible as stated in the MQL Documentation:
MQL4-library functions imported within other modules cannot have parameters initialized by default values.

Resources