In Objective C what does the "^{ ... }" mean? [duplicate] - iphone

This question already has answers here:
What does this caret ^ syntax, with void on either side mean? [duplicate]
(4 answers)
Closed 9 years ago.
I keep seeing lines of code with ^{ some code } in it... I thought that maybe it allowed to run a function inline similar to a lambda function. But I can not find any documentation on it. Could someone please enlighten me?

It is a block.
See the documentation.

Tis a block!
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1
To steal Apple's example:
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
printf("%d", myBlock(3));
// prints "21"

Yes, a block indeed...
Here is a tutorial for people who are beginners to blocks!

As Apple states in their documentation:
You use the ^ operator to declare a block variable and to indicate the
beginning of a block literal. The body of the block itself is
contained within {}, as shown in this example (as usual with C, ;
indicates the end of the statement):
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};

Related

What means the operator "??" in Dart/Flutter? [duplicate]

This question already has answers here:
What are the ?? double question marks in Dart?
(5 answers)
Closed 2 years ago.
I've seen this code and need an explanation for "??".
I know ternary operators like "?" and then the true-condition and after ":" the false/else condition.
But what means the double "??" ?
Thanks in advance
widget.secondaryImageTop ??
(widget.height / 2) - (widget.secondaryImageHeight / 2); ```
List of all dart operators
it's the coalesce operator.
a ?? b
means: if a is not null, it resolves to a. if a is null, it resolves to b.
SQL and a few other languages have this operator.
You example:
widget.secondaryImageTop ??
(widget.height / 2) - (widget.secondaryImageHeight / 2);
This will use widget.secondaryImageTop unless it is null, in which case it will use (widget.height / 2) - (widget.secondaryImageHeight / 2).
Source and detail, including dartpad where you can try things out with pre-populated examples:
https://dart.dev/codelabs/dart-cheatsheet
An example from that documentation, using the = sign as well.
the ??= assignment operator, which assigns a value to a variable only
if that variable is currently null:
int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3.
It means => if exists and if not null...

Is it possible to write a macro that expands an expression N times? (Where N is a constant) [duplicate]

This question already has answers here:
Is there a way to count with macros?
(4 answers)
Counting length of repetition in macro
(3 answers)
Using a macro to initialize a big array of non-Copy elements
(3 answers)
Closed 6 years ago.
Say we need to declare a fixed size array with values, where the size of the array is defined by a constant that may change depending on compile time settings.
So for example:
let my_array = expand_into_array!(j, ARRAY_SIZE, -v0[j] * f);
Where ARRAY_SIZE is a constant, for example:
const ARRAY_SIZE: usize = 3;
Could expand into something like...
let my_array = [
{let j = 0; {-v0[j] * f}},
{let j = 1; {-v0[j] * f}},
{let j = 2; {-v0[j] * f}},
];
Since the expression is a fixed size array, it may be possible to use pattern matching, for a limited number of items ... up to 32 for example.
Is it possible to write a macro that expands an expression N times, based on a constant integer?
Details...
Looking into this, I wrote a macro which defines an array, then fills it in, eg:
const ARRAY_SIZE: usize = 3;
macro_rules! expand_into_array {
($index_var:ident, $const_size:expr, $body:expr) => {
{
let mut tmp: [_; $const_size] = [0.0; $const_size];
for $index_var in 0..$const_size {
tmp[$index_var] = $body;
}
// TODO, check $body _never_ breaks.
tmp
}
}
}
pub fn negated_array(v0: &[f64; ARRAY_SIZE]) -> [f64; ARRAY_SIZE] {
expand_into_array!(j, ARRAY_SIZE, {
-v0[j]
})
}
This works as expected, and besides the wrinkle (that the $body expression could include a break). this works without problems.
However initializing the array to 0.0 isn't getting optimized out (changing this value shows up as changes when run with: cargo rustc --release -- --emit asm
I'd rather not use unsafe { std::mem::uninitialized }.
Update, from asking another question, it seems macros can only match against literals, and not constants.
So this is simply not possible with macros in Rust.

Proper swift 2.2 syntax with for loop [duplicate]

This question already has an answer here:
Replace c style for-loop in Swift 2.2.1
(1 answer)
Closed 6 years ago.
I wrote the following code in swift(2.2):
for var i = 2; sqrt(Double(num)) >= Double(i); i += 1 {.....}
However, I keep getting a warning message which reads "c-style for statement is deprecated and will be removed in future version of swift".
So, what is the proper way of writing the same loop is "Swift style"? Casting the value of num as double gives me error with "Swift style". Any suggestions?
Thank you.
You can refactor your deprecated C-style for loop using the for-in construct
for i in 2...Int(sqrt(Double(num))) { }
However if you really want to go essential try defining this operator to find the square root of an int rounded down to the closest int.
prefix operator √ {}
prefix func √ (number: Int) -> Int {
return Int(sqrt(Double(number)))
}
Now you can write your for loop this way
for i in 2...(√num) { }
The For loop in Swift 2.2 is re-designed for use in iterating over a sequence, such as ranges of numbers, items in an array, or characters in a string. The condition in your For loop is not easily converted into sequence or range, so it would be best to re-write it as a While loop.

How to decleare char *const argv[] in swift [duplicate]

This question already has answers here:
How to pass an array of Swift strings to a C function taking a char ** parameter
(2 answers)
Closed 7 years ago.
I am trying to make a c style array of chars pointer like this:
*const argv[]
I figured out i have to use UnsafePointer<UnsafeMutablePointer<Int8>>
but i don't know how to initialize it.
How can i map this normal Array to UnsafePointer<UnsafeMutablePointer<Int8>> :
let argv = ["/usr/bin/printf", "BBB"]
Thank you
The easy way is to let Cocoa form the C strings for you:
let args = ["/usr/bin/printf","BBB"]
var cs = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.alloc(2)
for (ix,s) in args.enumerate() {
cs[ix] = UnsafeMutablePointer<Int8>((s as NSString).UTF8String)
}
var cs2 : UnsafePointer<UnsafeMutablePointer<Int8>> = UnsafePointer(cs)
Beware; cs does not contain copies. Its pointers are pointing right into the strings in args.

How do I call a C function from a Perl program? [duplicate]

This question already has an answer here:
Calling C function from Perl within embedded C application
(1 answer)
Closed 9 years ago.
test.c:
int sum(int a, int b)
{
return (a+b);
}
test.pl:
# how do I call sum here?
use Inline C => <<'__END_OF_C__';
int sum(int a, int b)
{
return (a+b);
}
__END_OF_C__
say sum($x,$y);
You'll need XS. It's quite involved, so it's best to look at the official manual and tutorial pages for it:
http://perldoc.perl.org/perlxstut.html
http://perldoc.perl.org/perlxs.html