Swift out of bounds array loop / catching exceptions? - swift

In my swift game i loop through 2 arrays that run every frame(spritekit) like so
override func update(currentTime: CFTimeInterval) {
for (i, value) in enumerate(presents) {
for (ii, pvalue) in enumerate(portals) {
if(blah == true) {
presants.removeAtIndex(i)
}
//There is also some code that waits 1 second then runs
portals.removeAtIndex(ii)
}
}
}
As you can see in the inner loop I sometimes remove. But this sometimes crashes with the error, fatal error: Array index out of range and I am not sure how this is happening. My 2 theories are 1. Since it runs every frame and one has a delay it might be already in the next loop when it is removed OR 2. Since the loop iterates the arrays i am removing from the loop might not reset after it is removed. I could use a try catch( Pure swift no Obj-C ) but all the examples are for throwing errors I want to ignore them.
So my question is: Can I reset the loop or can I implement a try catch in pure swift?

You are removing items from your collections as you are iterating over them. Don't do that. Instead, you could collect the indices you want to remove in an array, and then remove them in reverse order (from largest to smallest) after you are done with the loops above.

Related

Are lists ensured to be processed in order by a for loop?

In a Flutter application, is the order of a list always ensured?
List<MyClass> myObjects;
// ... many add operations
int i = 0;
for(MyClass myObject in myObjects) {
assert(myObject == myObjects[i]); // will it always go through?
i++;
}
If not, what is the most efficient way to ensure a list is maintained and processed in the right order?
Yes. for-in in Dart works on Iterable objects. (It is not like for-in in JavaScript which iterates over object properties, which could be in some indeterminate order.)
From the List documentation:
Lists are Iterable. Iteration occurs over values in index order.

How to map over an array, use an if clause, and filter out the bad data?

I have an array of words, some may or may not have typos.
potentialWords = ["hello", "lkasjdf", "hunry"]
What I want to do is, return an array of all valid words, and also those words that were able to be autocorrected using a function I created correctWord. It returns an array of potential matches. so "hunry" might return ["hungry", "hurry"]. I will select the first index for the best guess.
Some words cannot be corrected however! e.g. "lkasjdf" will not find any corrections, but "hunry" will.
I was trying something like:
potentialWords.map {
if correctWord($0) != nil {
return correctWord($0)[0]
}
}
of course this will complain and say that I need a return outside the if clause. I can filter the list based on if the word can be corrected, and then map over the filtered list, re-checking which words need to be corrected, but this runs the correctWord function way too many times, and it is very sensitive.
I would like to be able to do one single pass through, and return an array of all valid words, and also corrected words.
P.S. I am calling correctWord twice in the map function for brevity, but of course I would assign correctWord($0) to a variable, and then if it isn't nil, take the first index and add it to the new list.
I think you're after flatMap. It's the same as map except it will also filter out any nil values.
potentialWords.flatMap { correctWord($0)?.first }

Array of observables where the output of each is the input of the next

I'm trying to use RxSwift to execute actions on multiple data sources. However, I have no idea how to accomplish the following.
I have an array of observabless where the output of each, should be the input of the next. So, I want to do something like, get the first observable, wait for the result and pass it to the next, all the way to the end of the array and return one final value.
Is that possible? Thanks in advance.
*** Update: Ok, I'll be more specific as requested.
The 'observables' I'm using in the array, are custom. I use a function that returns Observable.create { ... }. Inside the closure, I run an asynchronous operation that transforms the value and then send the result to the observer before completing. That resulting value, must pass to the next observable, and so on to the last observable in the array to get a final value.
The observables may send multiple values, but they must pass from one observable to the next like an assembly line.
It is difficult to know exactly what you are asking for, since Observables do not exactly have inputs but I think this is a common problem.
You may be looking for a combination of the concat or reduce operators, which allow you to accumulate data from the values emitted from an Observable. See ReactiveX's documentation for Mathematical and Aggregate Operators.
Hopefully this can get you started:
// "I have an array of observables..."
let one = Observable.deferred { Observable.just(1) }
let two = Observable.deferred { Observable.just(2) }
let observables = [one, two]
// "the output of each, should be the input of the next"
// this is problematic, because observables do not strictly have inputs.
let resultsFromEach = Observable.concat(observables)
resultsFromEach
.reduce(0) { result, next in
result + 1
}
.debug("result")
.subscribe()

How can I chain promises in Swift inside a loop?

I'm building a Swift-based iOS application that uses PromiseKit to handle promises (although I'm open to switching promise library if it makes my problem easier to solve). There's a section of code designed to handle questions about overwriting files.
I have code that looks approximately like this:
let fileList = [list, of, files, could, be, any, length, ...]
for file in fileList {
if(fileAlreadyExists) {
let overwrite = Promise<Bool> { fulfill, reject in
let alert = UIAlertController(message: "Overwrite the file?")
alert.addAction(UIAlertAction(title: "Yes", handler: { action in
fulfill(true)
}
alert.addAction(UIAlertAction(title: "No", handler: { action in
fulfill(false)
}
} else {
fulfill(true)
}
}
overwrite.then { result -> Promise<Void> in
Promise<Void> { fulfill, reject in
if(result) {
// Overwrite the file
} else {
// Don't overwrite the file
}
}
}
However, this doesn't have the desired effect; the for loop "completes" as quickly as it takes to iterate over the list, which means that UIAlertController gets confused as it tries to overlay one question on another. What I want is for the promises to chain, so that only once the user has selected "Yes" or "No" (and the subsequent "overwrite" or "don't overwrite" code has executed) does the next iteration of the for loop happen. Essentially, I want the whole sequence to be sequential.
How can I chain these promises, considering the array is of indeterminate length? I feel as if I'm missing something obvious.
Edit: one of the answers below suggests recursion. That sounds reasonable, although I'm not sure about the implications for Swift's stack (this is inside an iOS app) if the list grows long. Ideal would be if there was a construct to do this more naturally by chaining onto the promise.
One approach: create a function that takes a list of the objects remaining. Use that as the callback in the then. In pseudocode:
function promptOverwrite(objects) {
if (objects is empty)
return
let overwrite = [...] // same as your code
overwrite.then {
do positive or negative action
// Recur on the rest of the objects
promptOverwrite(objects[1:])
}
}
Now, we might also be interested in doing this without recursion, just to avoid blowing the call stack if we have tens of thousands of promises. (Suppose that the promises don't require user interaction, and that they all resolve on the order of a few milliseconds, so that the scenario is realistic).
Note first that the callback—in the then—happens in the context of a closure, so it can't interact with any of the outer control flow, as expected. If we don't want to use recursion, we'll likely have to take advantage of some other native features.
The reason you're using promises in the first place, presumably, is that you (wisely) don't want to block the main thread. Consider, then, spinning off a second thread whose sole purpose is to orchestrate these promises. If your library allows to explicitly wait for a promise, just do something like
function promptOverwrite(objects) {
spawn an NSThread with target _promptOverwriteInternal(objects)
}
function _promptOverwriteInternal(objects) {
for obj in objects {
let overwrite = [...] // same as your code
overwrite.then(...) // same as your code
overwrite.awaitCompletion()
}
}
If your promises library doesn't let you do this, you could work around it by using a lock:
function _promptOverwriteInternal(objects) {
semaphore = createSemaphore(0)
for obj in objects {
let overwrite = [...] // same as your code
overwrite.then(...) // same as your code
overwrite.always {
semaphore.release(1)
}
semaphore.acquire(1) // wait for completion
}
}

iPhone game: enumerating over enemies

So I'm constantly checking collision detections and whenever an enemy gets killed I remove it from the array I'm iterating over.
This is giving me enmueration mutation exceptions, what is the normal way to solve this?
People talked about making copies of the array, but when I'm iterating over it every fraction of a second that idea seems ridiculous.
I agree with the comment of Till. Do something like that, if self.enemies is your original mutable array
NSArray * enemiesToRemove = [[NSMutableArray alloc] init];
for (Enemy * enemy in self.enemies) {
if ([enemy colidesWithBullet]) {
[enemiesToRemove addObject:enemy];
}
}
[self.enemies removeObjectsInArray:enemiesToRemove];
[enemiesToRemove release];
Basically you cant remove or add from the array while its being enumerated, and since you seem to be iterating the array often then you will probably get this exception a lot, one choice is to lock your array while you iterate and while you remove using a #synchronized block on the array, this will guarantee that you wont be modifying the array while iterating...Down side to this approach is that you will have the iterations and add/remove operations waiting on each other...
#synchronized(myArray)
{
//iterate through myArray
}
#synchronized(myArray)
{
//mutate the array
}
Removing items from an array that you are itterating over can be tricky. You can do something like Zoleas suggested or you can avoid enumerating the list and start at the last element and check backwards to the first element removing elements that need removed. This way you can ensure that you are never removing an element that will effect the index of later elements.
for(int i=[array count]-1; i >=0 :i--)
{
bool needsRemoved = /* your code here */;
if (needsRemoved)
{
[ary removeObjectAtIndex:i]
}
}