Objective-C to Swift Equivilant [closed] - objective-c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to figure out how to write this block method in swift. I can't seem to get the closure syntax right:
[self.colorPickerView setDidChangeColorBlock:^(UIColor *color){
self.selectedColor.backgroundColor = self.colorPickerView.color;
}];
Thanks in advance!
What i tried:
self.colorPickerView.didChangeColorBlock({
(color: UIColor) in self.selectedColorView.backgroundColor = self.colorPickerView.color
})
Final Solution:
self.colorPickerView.didChangeColorBlock = {
(color: UIColor!) in
self.selectedColorView.backgroundColor = color
}

You probably need to use
self.colorPickerView.didChangeColorBlock = { ...
instead of
self.colorPickerView.didChangeColorBlock({ ...
Since didChangeColorBlock is a property, not a method.

Related

awk function to compute median of an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to have an awk function to compute the median of an array (may not be sorted).
median of column with awk
The above is a similar post but it is not the same as this one because the input on the above post is a sorted stream.
Could anybody show me the function to compute the median of an array?
I got the following solution. I am not sure if it would fail in a corner case. If it does, please let me know.
function median(x, n, d) {
n = asort(x, d, "#val_num_asc")
if(n == 0) return "NA"
if(n%2) {
return d[(n+1)/2]
} else {
return (d[n/2] + d[n/2+1])/2
}
}

Why can you not specify var/val loops in Kotlin? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why can't you specify a val or var type in for loop's in Kotlin. For example, I would like to be able to do
for (var i in 0...data.size - 1) {
for (j in 0..bytes.size - 1) {
bytes[j] = data[i++]//cant do i++ in current kotlin because "i" is val
}
//do stuff
}
But instead I have to do this
var i = 0
while (i < data.size) {
for (j in 0..bytes.size - 1) {
bytes[j] = data[i++]
}
//do stuff
}
Your example is slightly different from Java's typical for(int i=0;i<data.size;i++) example.
In the Kotlin version 'i' is actually an element of the range in which case i++ doesn't make sense. It just so happens that the range you have is a list of indexes.
The way you are using the Kotlin for loop is much closer to Java's foreach loop for(i : indexes).
I think that because Kotlin is a language that tries to make easy to respect most of the functional programming concepts, it prefers to prohibit that sort of behaviour. Also, a possible problem that your initial code could encount is an OutOfBoundException in the situation where the bytes array has more elements than the data array.

Confused to get that 2^(n^2 )=Θ(2^(n^3 ))? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Can anyone help me to understand that Is 2^(n^2 )=Θ(2^(n^3 )) ? it will be great if also provide the proof for this. As per my view this does not need to be equal.
The given assumption is not true:
First of all, 2^(n^2) is a function and Theta(2^(n^3)) is a set of functions, so it would be correct to say that 2^(n^2) ∈ Theta(2^(n^3)). The = is just a common abuse of notation, but it actually means ∈. To find out whether that statement is true, solve the following limit:
lim (n->infinity) of (2^(n^2)) / (2^(n^3))
If the result is 0 or infinite then the function does not belong to that particular Theta class. If it is some other value, it does belong to that class.

What is "fabs" in Objective C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Found this bit of code...
if(fabs([tempPlacemark getCoordinate].latitude-latitude) < latDelta && fabs([tempPlacemark getCoordinate].longitude-longitude)<longDelta )
...
refers to this in Math.h:
extern float fabsf(float);
extern double fabs(double);
extern long double fabsl(long double);
So what am I looking at?
double fabs( double ) - returns the absolute value of the argument
NSLog(#"res: %.f", fabs(10)); //result 10
NSLog(#"res: %.f", fabs(-10)); //result 10
found here.

Why does this code work in Objective-C? I'm getting BAD_ACCESS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
int RandomSource_next(int bits, double* seed) {
*seed = (((long long) *seed * 0x5DEECE66DLL) + 0xBLL) & ((1LL << 48) - 1);
return (int)((signed long long) *seed >> (48 - bits));
}
I think it got something to do with the address.
Most probably you are passing incorrect address as seed. Maybe you're passing not an address but a value?
The following should work
double seed = 0;
RandomSource_next(48, &seed);
The following should crash
RandomSource_next(48, 0);