Finding out whether ObjectAtIndex returns an object - ios

Is there a way to write some sort of catch statement around ([MyArray ObjectAtIndex:myindexpath.row])so that I can run this without throwing an exception?
In other words, I want to write this sort of expression:
if ([MyArray ObjectAtIndex:myindexpath.row]) {
// do some stuff if the object is in the array
else {
// do some other stuff
}

Sure: use logic & maths.
if (index < myArray.count) {
// ...
}

Related

Initialize variable in if condition - Flutter/Dart

Is it possible to do something like the following? aka, initialize a variable inside an if statement condition?
Reasoning:
I have a network call that will fetch data, and I'd like to avoid the following options:
Calling it unless the first condition is false.
Calling it twice, once to fetch the data to check the conditional & once to use the data inside the condition
Having to nest if statements
So initializing the variable inside the conditional block seems like the cleanest solution.
if (condition1) {
// Do something
} else if ( (String foo = await getBar()) == "not bar"){
// Do something with foo
} else {
// Fallback condition
}
One option would be to declare the variable before the condition like so:
String foo; // Declare variable here
if (condition1) {
// Do something
} else if ((foo = await getBar()) == "not bar") {
// Do something with foo
} else {
// Fallback condition
}

what is alternative of for in in darjs?

I am looking for the alternative of javascript for in in dart:js?
for example:
if('addEventListener' in event) {
event.addEventListener(change);
}
I used is operator, but it's throwing an error in Safari becouse addEventListener does not exist in event.
if(event.addEventListener is Function) {
event.addEventListener(change);
}
Checking whether an object supports a specific method is not something you do in Dart. You should check that the object implements an interface which has that method.
In this example, you probably need:
if (event is EventTarget) {
event.addEventListener("change", change);
}
If you think that the object might support the function, but you don't actually know which interface it gets the function from, then you can do what you try here, using a dynamic lookup, but you need to catch the error you get if the function isn't there.
dynamic e = event; // if it isn't dynamic already.
Object addEventListener;
try {
addEventListener = e.addEventListener;
} on Error {
// ignore.
}
if (addEventListener is Function) {
addEventListener(...);
}

Checking if array contains two objects

I'm attempting to implement the containsObject but with two parameters, is this possible?
Currently I've got:
if ([ myArray containsObject:#"Object1", #"Object2"]){
return result;
} else {
return NO;
}
and apparently there's too many arguments. I've delved through Apple's docs but I'm yet to find anything. Any suggestions?
Why not just do this?
if ([ myArray containsObject:#"Object1" ] && [ myArray containsObject:#"Object 2" ] ){
return result;
} else {
return NO;
}
There is too many arguments, containsObject is for a single object. (You can read its official documentation here) To fix your problem, use the && operator and call containsObject on each object individually.
if ([myArray containsObject:#"Object1"] && [myArray containsObject#"Object2"]){
return result;
} else {
return NO;
}
You will have to evaluate them individually. Example:
bool MONNSArrayContainsAllObjectsIn(NSArray* const pArray, NSArray* const pSought) {
assert(pArray);
assert(pSought);
assert(0 < pSought.count);
for (id at in pSought) {
if (false == [pArray containsObject:at]) {
return false;
}
}
return true;
}
Then your code above becomes:
return MONNSArrayContainsAllObjectsIn(myArray, #[#"Object1", #"Object2"]);
If you are working with a known number of elements (2 in this case), then you can avoid creating the temporary array -- if you prefer to make that optimization and write out all variants you need, including parameters. Other answers detail this approach.
If you have large arrays and many comparisons to perform, NSSet may be better suited for your task.

Returning NULL Structure

I am calling a function which returns a structure of the type CvBox2D, however I want to check for an error in the function and return NULL if there is an error.
CvBox2D function()
{
...
if(ERROR)
return NULL;
...
}
I am getting an error : cannot convert from 'int' to 'CvBox2D'
Your function return type is CvBox2D, so you can't convert a (NULL) pointer to it.
If you really need to return "nothing" if the check inside the function fails, you can change the return type to a pointer to CvBox2D:
CvBox2D* function()
{
...
}
You will also have to change the way the returned object is created inside your function.
Note that using raw pointers in C++ usually isn't a good idea.
Take a look at std::shared_ptr (available in C++11) if you think you really have to use pointers.
If you want to return some error code, you can do the following:
int function(CvBox2D* output) {
// code...
// Assign to struct.
output->center = ...;
if (error) {
return RC_ERROR_FOO;
}
return RC_OK;
}
Then you call this function using a struct you've already allocated (for example, on the stack):
{
CvBox2D myBox;
int retval = function(&myBox);
if (RC_OK == retval) {
printf("Good! Angle of box: %g", myBox.angle);
} else {
printf("Error: %d", retval);
}
}
Where RC_OK, RC_ERROR_FOO are defined as constant integers, or better, as an enum (if you're using C++).
The other answers solve your problem, but if you want to keep the signature of your function, instead of returning an error code, you should throw an exception.

Grails 2 - How to dynamically call multiple datasources

I have two named data sources in my Grails app (Grails 2.0.3)...
dataSource_a {
// ...
}
dataSource_b {
// ...
}
I'd like the ability to dynamically change what datasource I'm accessing, based on some kind of parameter. I could do something like this...
def findPeople(datasource) {
if (datasource == 'a') {
return Person.a.list()
} else if (datasource == 'b') {
return Person.b.list()
}
}
What I was really hoping to be able to do, though, is something like this...
def findPeople(datasource) {
return Person."$datasource".list()
}
Unfortunately, I get an error when I try and do that. "Fatal error occurred apply query transformations: null 1 error".
Any thoughts on how to accomplish this? Or am I just stuck with if/switch blocks?
I figured it out, this is how you have to do it.
def findPeople(datasource) {
def p = People.class
p."${datasource}".list()
}
For some reason, if you call it like that, it works.

Resources