What's wrong with my code? (obj c tweak development) - ios

So, I'm new to tweak development and obj c and I'm trying to change the style of dock with this code
%hook SBWallpaperEffect
-(void)setStyle: (NSInteger)arg1{
arg1 = 5;
}
%end
But it doesn't work. What am I doing wrong?

OK, I think this is the typical kind of pointer problem many programmers face. You are passing a NSInteger to the function, and arg1 is a copy of the value that is passed in. In other words, no matter how you change arg1 inside the function, the original variable does not change.
To make things more clear, look at the following code snippet:
NSInteger myInteger = 0;
NSInteger arg1;
arg1 = myInteger;
arg1 = 5;
// myInteger is still 0!
Changing arg1 does not change the value of myInteger, and that's what you're doing in your function. The arg1 copies the value of the integer passed in, be set to 5, and then gets released at the end of the function.
Instead, try this:
-(void)setStyle:(NSInteger *)arg1{
arg1 = 5;
}
and then call the function like this:
NSInteger myInteger = 0;
[self setStyle:&myInteger];
//myInteger is now 5!
Now you will get the result you want.
If you don't want to deal with these messy pointer stuff, use the returning value of the function to pass the number like this:
-(NSInteger)getStyle:(NSInteger)arg1{
// do some calculation
arg1 += 5;
return arg1;
}
or something like this (with no parameters):
-(NSInteger)getStyle{
return 5;
}
and call it like:
myInteger = [self getStyle:myInteger];
// or
myInteger = [self getStyle];
Now myInteger will also be 5. Hope this helps :)

Ok so i figured out that im using NSInteger instead of long long. Like this:
(void)setStyle:(long long)arg1;
Also, i was using
SBWallpaperEffect
Instead of
SBWallpaperEffectView
And i should've set the value inside %orig
So, this is the right way to write it:
%hook SBWallpaperEffectView
- (void)setStyle:(long long)arg1{
arg1 = 5;
%orig(arg1);
return;
}
%end

Related

Loop over a function and store as list

I have the following function that clicks a checkbox with splash:
local get_dimensions = splash:jsfunc([[
function () {
for i=1, 5 do
var rect = document.querySelector(string.format('checkbox[number="%d"]'), i)
.getClientRects()[0];
return {[rect.left]: rect.top};
end
}
]])
However, I cannot store a list from the loop into a single variable like rect, so how do I store a list into a variable, and when I return it, it should return a list of values?
Something similar to python, ie.:
def stuff():
rect = []
for i in range(5):
rect.append([...])
return rect
Let's take at your code.
You call splash:jsfunc. This function converts JavaScript into a Lua callable. It takes a single argument. A string that defines a JavaScript function.
yourString = [[
function () {
for i=1, 5 do
var rect = document.querySelector(string.format('checkbox[number="%d"]'), i).getClientRects()[0];
return {[rect.left]: rect.top};
end
}
]]
This looks like some weird mix of Lua and JavaScript.
This is how a JavaScript for loop looks like
for (let i = 0; i < 10; i++) {
// some code
}
This is how a Lua numeric for loop looks like:
Also what's the point of using return in a loop without any condition? This will be executed after the first cycle with i = 1. That's probably not what you want.
So fix the loop so it is JavaScript. Create a Object or Array and return that after the loop is complete.

ThinkOrSwim function equivalent in MQL

I have never heard of ThinkOrSwrim till yesterday when someone asked me to convert a ThinkOrSwim script to an MQL4 indicator.
A part of the code is as follows:
input length = 21;
input price = close;
input ATRs=1;
input trueRangeAverageType = AverageType.WILDERS;
def flag;
def EMA = ExpAverage(close, length);
def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
I want to ask you to kindly check and let me know if my understanding is correct.
input ATRs=1; // This should be a multiplier for ATR, then I think I should give it a double
//type for more flexible control.
input trueRangeAverageType = AverageType.WILDERS;
//As far as I understood, wilders is the same as SMMA in MQL.
.
def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
Here is the main piece of this code which I need your help with.
My understanding is as follows
ATRs ==>> Just a multiplier
I think the rest of this line is calculating the ATR, right?
If so, then I can see that I cannot simply convert this to iATR (in mql), because we are not able to choose MA Methode of ATR in mql4.
Then I think first I have to put the "True Range" of each bar in an array and then use this array as a price source to get the averages.
MQL4:
for(int i = 0; i < rates_total; i++)
{
data[i] = iATR(_Symbol, TF_1, 1, i);
}
for(int i = 0; i < limit; i++)
{
ExtBuffer[i] = iMAOnArray(data, 0, Inplenght, 0, InpMAMethod, i);
}
If I'm in the right way yet, Then I think the iATR period has to be 1, to have the TrueRange of each bar and not the average of the TrueRanges.
And then have the variable length (from thinkOrSwim inputs) as the period parameter for iMAOnArray.
I would appreciate any help with it.
Regards
Edit:
I forgot to ask you something,
why should the programmer who wrote this thinkscript code call this variable shift1?

ArrayFire seq to int c++

Imagine a gfor with a seq j...
If I need to use the value of the instance j as a index, who can I do that?
something like:
vector<double> a(n);
gfor(seq j, n){
//Do some calculation and save this on someValue
a[j] = someValue;
}
Someone can help me (again) ?
Thanks.
I've found a solution for this...
if someone had a better option, feel free to post...
First, create a seq with the same size of your gfor instances.
Then, convert that seq in a array.
Now, take the value of that line on array (it's equals the index)
seq sequencia(0, 200);
af::array sqc = sequencia;
//Inside the gfor loop
countLoop = (int) sqc(j).scalar<float>();
Your approach works, but breaks gfors parallelization as converting the index to a scalar forces it to be written from the gpu back to the host, slamming the breaks on the GPU.
You want to do it more like this :
af::array a(200);
gfor(seq j, 200){
//Do some calculation and save this on someValue
a[j] = af::array(someValue); // for someValue a primitive type, say float
}
// ... Now we're safe outside the parallel loop, let's grab the array results
float results[200];
a.host(results) // Copy array from GPU to host, populating a c-type array

invoking block in array with parameters

I've seen the answer about invoking a block that is stored in an array, but I can't get it to work with parameters.
I store the array an a part of an object, then when it's in a method, I want to invoke it, however, I need parameters.
Also, is there any limit to the parameters.
Lastly, I'd rather not use the extra storage to the variable, so invoking directly while in the array would be better.
__block int x = 123; // x lives in block storage
void (^printXAndY)(int) = ^(int y) {
x = x + y;
NSLog(#"X and Y: %d %d\n", x, y);
};
self.blocks = #[printXAndY];
printXAndY(10); // this works
void(^block)(void) = self.blocks[0];
block(); // this works
block(10); // this doesn't work
[self.blocks[0] invoke ];
The problem is this line:
void(^block)(void) = self.blocks[0];
You are declaring 'block' to take no parameters and return nothing. If you want the block to take a parameter, you need to declare it like this:
void(^block)(int) = self.blocks[0];
Note that block(); will no longer work. And when you declared the block incorrectly, that line was undefined behavior.

How does return i++ work?

This is a question about the ++ operator. It is a question that doesn't solve a problem but I believe that it would be very informative to know the answer. So, while StackOverflow is a source of information, I would like to append the available knowledge here!
So, the problem:
int i = 0;
int getNextInt(){
return i++;
}
This is it. The question is how the ++ operator is implemented in the function call stack and where the increment actually happens when we call that function in our code.
I am really wondering a long time!
It happens after the function returns it's value. In this case, it returns 0 and then increments the variable, so the next time it will return 1
I have no idea what the compiler actually does with regard to the stack, but if it chose to, the compiler could easily rearrange it to this:
int getNextInt() {
int temp = i;
i++;
return temp;
}
i++ is the same as saying i = i+1
It is just a shorthand way of redefining the variable.

Resources