cydia-substrate tweak development - giving errors on make - mobile

I am new to making mobile substrate tweaks and I wanted to hook this function but having a bit of trouble.
%hook classname
- (void)function:(BOOL) {
%orig;
return TRUE;
}
%end
but when i try to MAKE this it gives me an error. void function should not return a value.
I just want to change the BOOL to always return true.
Thanks.

Your aim is vague a bit. The error is because your function header is
- (void)function:(BOOL) foo {
%orig;
return YES;
}
This means function function has a boolean argument and RETURNS NOTHING. So you can't write return YES in your function. This line causes the error.
If your goal is to return the value of the original function with YES argument no matter what foo is, you have to rewrite your function like below:
- (void)function:(BOOL) foo {
%orig(YES);
}

Related

Redux - Async-Await response undefined but 'works' [duplicate]

I'm new to JavaScript, and I try to play around with it to understand all in-and-outs. I write
function greet() {
console.log("Hi");
};
console.log(greet());
And the result of it in the console is
> Hi app.js:2
> undefined app.js:4
I assume this is because greet() inside console.log first calls the function, which prints out "Hi". We get first line of log. But where did the second line come from?
Then I thought because Hi is overall result of greet(), then console.log basically calls variable Hi, but in this case the result would be is not defined, not undefined
In JavaScript, if nothing is returned from the function with the keyword return then undefined is returned by default.
var data = greet();
console.log(data);// undefined, since your function does not return.
Is equivalent to:
console.log(greet());
The second output is the returned result from the function. Since you are not returning anything from the function hence prints undefined.
To print 'Hi' in the second console you have to return that from the function.
function greet() {
console.log("Hi");
return 'Hi';
};
console.log(greet());
You should use a return keyword like this:
function greet() {
console.log("HI");
return "HI";
};
console.log(greet());
Or you can store it in a variable and return the variable:
function greet() {
const hello = ("HI");
return hello;
};
cosnole.log(greet());
,Because if you don't use return keyword and log the function to the console then it returns undefined.

Is there something in C that is analogous to out/out keyword of C#?

void extract_left_subtree(node *right_child)
{
while(right_child->right)
{
right_child = right_child->right;
}
printf("rightmost inside the funtion is %d\n",right_child->data);
}
in this function the last line is printing the correct value.
node *right_child=root;
extract_left_subtree(right_child);
printf("rightmost child is %d\n",right_child->data);
But here I'm getting some garbage value.
I know what the problem is, I know why it's happening, the only thing I don't know is how to rectify this?
There are keywords like ref and out in C# which can be used to achieve the same but the issue is, how can we do the same in C language?
I don't want to return values from the method please
I don't want to return values from the method please
If you don't want to return a value you can do:
void extract_left_subtree(node **right_child)
{
while((*right_child)->right)
{
(*right_child) = (*right_child)->right;
}
printf("rightmost inside the funtion is %d\n", (*right_child)->data);
}
and call it like:
extract_left_subtree(&right_child);
This passes the address of right_child to the function and then the function can directly update the value of right_child

How to fix "expected call-signature to have a typedef" error

I have a switch case function inside my render JSX element that I am using in another const value based on the return values.
I tried looking for a solution to my issue online but none of the answers I found are helping my issue, even after trying them on my code.
function carouselClass(transitionTypeValue: any) {
switch(transitionTypeValue) {
//Different cases and their return values
}
}
const classes = xyz(carouselClass(transitionType)) //Sample of where I'm using my switch function
The code runs fine on Typescript side but I get a linting error which says "typedef expected call-signature: 'carouselClass' to have a typedef"
I tried my best to give enough context, please let me know if more is required as this is my first time posting a question.
Linter giving 'Missing Type definition' warning. Check this out-
https://github.com/palantir/tslint/blob/master/src/rules/typedefRule.ts for details.
Solution: You need to specify the return type for function explicitly
function carouselClass(transitionTypeValue: any):any {
switch(transitionTypeValue) {
//Different cases and their return values
}
}
I think you should add return type if you are calling function and returning something.
function carouselClass(transitionTypeValue: any):any {
switch(transitionTypeValue) {
//Different cases and their return values
}
}
const classes = xyz(carouselClass(transitionType))
You should avoid using type any where ever you can.
Avoid returning any or value with multiple types,
but if you still want to return multiple types of value, try to find similarity and create Enum type or Wrapper Interface, or you can try to create generic function.
interface IClassType extend IClassA, IClassB {}
function carouselClass(transitionTypeValue: any): IClassType {
switch(transitionTypeValue) {
// Different cases and their return values
// Avoid returning multiple types,
// but if you still want to return multiple types of value,
// try to find similarity and create Enum type or Wrapper Interface
}
}
const classes: xyz<IClassType> = xyz(carouselClass(transitionType))

Do the return codes in Backbone.Event matter?

Do the return codes for the callback given to Backbone.Event matter at all? Is there a convention for them?
obj.on( "event_name", function () { return true; } )
vs
obj.on( "event_name", function () { return false; } )
The only way you can really be sure is to check the backbone source. Here's the function that internally calls events:
https://github.com/jashkenas/backbone/blob/master/backbone.js#L303
It's a little hard to read because it's been heavily optimized. The actual call occurs on one of the five instances of (ev = events[i]).callback.call. In any of those cases, the return value is not used.
So it should be pretty clear that Backbone just discards the return value of the callbacks.

Return Value for "should cancel"

I have a method DoCleanUp(), which will ask user to proceed and then clear current workspace. It will return if user choose to cancel this process.
My question is, which signature is best to indicate a "cancel"?
bool DoCleanUp(); // return false to indicate canceled.
bool DoCleanUp(); // return true to indicate this method should be canceled.
void DoCleanUp(bool& cancel); // check parameter 'cancel' to see if this method was canceled.
UPDATE: As for the language, it's C++\CLI or C#.
UPDATE2: Now suppose I have to save a file in the DoCleanUp method. I'll prompt a dialog ask user whether to save/not save/cancel the file. Based on the answers, here is what I came up:
void DoCleanUp();
DialogResult AskToSaveFile(); // return yes/no/cancel
void DoCleanUp( bool saveFile );
Usage:
void DoCleanUp()
{
DialogResult result = AskToSaveFile();
if( result == DialogResult::Cancel ) return;
bool saveFile = (result == DialogResult::Yes) ? true : false;
DoCleanUp( saveFile );
}
Then by calling DoCleanUp(), you know user will have the opportunity to cancel;
By calling DoCleanUp(bool saveFile), you can control whether to save file without asking user.
Is that looks better?
This is a classic single responsibility problem.
The reason that you are unsure about the signature is that the method is doing 2 things.
I would create 2 methods:
bool CheckIfTheUserWantsToCancel()
void DoCleanUp()
EDIT
Based on the comments and edits to the question I would create a 3rd method:
void SaveFile()
The DoCleanUp would then first call CheckIfTheUserWantsToCancel, and then if not cancelled would call SaveFile.
IMHO this is much better than trying to remember that DoCleanUp with parameter false will save the file without asking the user, or was it the other way around?
Without more details I would say answer 1 is the best IMHO. Third is rather ugly since it requires more code for calling.
But maybe consider rewriting code to this
void CleanUp() {
switch (AskUser()) {
case ButtonOk: CleanUpDesk(); break;
case ButtonNo: break;
default:
case ButtonCancel: CancelCleanUpDesk(); break;
}
}
This seems to in the spirit of single responsibility. My code somehow breaks your problem into two steps: asking user and performing action.
I would use your 1 version.
bool DoCleanUp(); // return false to indicate canceled.
The assumption is, that it returns true when the cleanup is done. Returning false would indicate a 'Error' state. It might even make sense to return an int. In this case the convention usually is that 0 represents success and everything else is an error code.
Regardless of what you decide, document what your return values mean!
The confusing bit is the calling it DoSomething(), when it might not do anything. How about
if (QueryCleanup()) // boolean
DoCleanup(); // void
More verbose but clearer, even without seeing the declaration.
You should not use a boolean for statuses (or status messages). Create an Enum:
public Enum CleanupStatus
{
Ok = 0,
Cancel
}
This way it is more clear what the return value is ... and if you need to add more statuses, you can.
(This is all from Code Complete 2, you should read it if you haven't yet.)
You have two requests basically. The outer request is to create a new workspace. The inner request is to save the current workspace. You want to return true if the outer request continues and false if the outer request is aborted. The action of the inner request is not important to the outer request and so should be some kind of delegate/functor/closure.
Make a class to genericize this:
class YesNoCancel {
string question; // question to ask the user about the inner state
delegate doit; // function to call to
delegate dontdoit;
public:
YesNoCancel(string question, delegate doit, delegate dontdoit = null) {...}
bool run() {
switch (AskUser(question)) {
case ANSWER_YES: doit(); return true;
case ANSWER_NO: return true;
case ANSWER_CANCEL: if (dontdoit) dontdoit(); return false;
};
//usage
void NewWorkspace() {
if (m_workspace) {
YesNoCancel ync("Save current workspace?", saveworkspace);
if (!ync.run()) return;
}
// new workspace code
}
void CloseApp() {
YesNoCancel ync("Save current workspace?", saveworkspace);
if (ync.run()) ExitApplication();
}
I believe option three gives the most clarity. When you have the bool as a return type it is not immediately clear what it is used for.
I usually go with
bool DoCleanUp(); // Returns true if cancel
but mostly it depends on whether the calling code looks like this:
if (DoCleanUp()) {
// Do cancel up code
}
or:
if (DoCleanUp()) {
// Do non-cancel post clean up code
}
Basically I try to make my tests not have to use a ! or language equivilent as I find it hard to see.
I definitely would not do number 3.
I prefer the third signature, only because by looking at it (without any extra documentation), I can tell more about what the method does. I would call the argument something more explicit, like processCancelled, though.

Resources