1

I am making a code that displays all the messages in a table view. To & From. The problem I am running into is the arrays are different lengths and when comparing them the loop stops at the shortest on instead of continuing.

What Im doing is taking the two different arrays & Sort them into 1 larger array thats a combination but sort them based on the time in comparison to the other.

I'm using Swift & I'm also using Parse to query the information.

I'll admit I'm not the best with arrays. Would this be a good time for a dictionary of sent messages & Recieved messages and then loop through?

  for var i = 0; i <= self.messagesPFObjectResults.count; i++ {

     let sentMessagesInfo = sentMessagesObject![i] //This Equals 7
     let recievedMessageInfo = recievedMessagesObject![i] // this equals 8

        if sentMessagesInfo.createdAt?.timeIntervalSinceReferenceDate >= recievedMessageInfo.createdAt?.timeIntervalSinceReferenceDate {

            self.messagesPFObjectResults.append(recievedMessageInfo)
            print("message recieved at: \(recievedMessageInfo.createdAt!)")


            print(false)

          } else if sentMessagesInfo.createdAt?.timeIntervalSinceReferenceDate <= recievedMessageInfo.createdAt?.timeIntervalSinceReferenceDate {

             self.messagesPFObjectResults.append(sentMessagesInfo)

             print("message sent at: \(sentMessagesInfo.createdAt!)")

             print(true)

          }

          print(i)                            
  }

2 Answers2

0

This line:

self.messagesPFObjectResults.append(<someObject>)

is problematic. When you are inside the for-loop, you should not modify the array you are iterating over.

Instead, you can define an empty array before your for loop, and append your objects to that array instead. After you have completed the for-loop, you can add the objects from your temporary array into self.messagesPFObjectResults.

dbn
  • 458
  • 5
  • 12
0

If you create a Class Message and subclass it into ReceivedMessage and SentMessage than it is really easy to sort them and create a total array.

See this example (with Integers instead of dates out if simplicity):

class Message {
    let date : Int

    init(date : Int) {
        self.date = date
    }

}

class ReceivedMessage : Message {

}

class SentMessage : Message {

}

// Create the arrays containing the Messages
var receivedMessages : [ReceivedMessage] = []
var sentMessages : [SentMessage] = []

// Just creating random objects
for i in 1...5 {
    receivedMessages.append(ReceivedMessage(date: i))
}

// Just creating random objects
for j in 1...3 {
    sentMessages.append(SentMessage(date: j))
}

// Create the array to hold all the Received and Sent messages i
var messages : [Message] = []
for receivedMessage in receivedMessages {
    messages.append(receivedMessage as Message)
}

for sentMessage in sentMessages {
    messages.append(sentMessage as Message)
}

// Create the array with all the sorted messages
let ordenedMessages = messages.sort {
    return $0.date < $1.date
}

print("Messages:")
for message in messages {
    switch(message) {
    case is ReceivedMessage : print("ReceivedMessage: \(message.date)")
    case is SentMessage : print("SentMessage: \(message.date)")
    default: break
    }
}

print("\nOrdened Messages:")

for message in ordenedMessages {
    switch(message) {
    case is ReceivedMessage : print("ReceivedMessage: \(message.date)")
    case is SentMessage : print("SentMessage: \(message.date)")
    default: break
    }
}

Run this in a Playground and see it for yourself.

Emptyless
  • 2,964
  • 3
  • 20
  • 30