1

i have two arrays of custom objects([InboundMessages],[OutboundMessages])

    struct InboundMessage {

        let senderName: String!
        let senderText: String!
        let senderProfileImageName: String!
        let timeAndDateMessageReceived: NSDate!
        let hasBeenRead: Bool!
        let messageUUID = NSUUID().UUIDString
        let conversationUUID: NSUUID = NSUUID()

    }
    struct OutboundMessage {

        var replyText: String!
        var messageUUID = NSUUID().UUIDString
        var timeAndDateMessageSent: NSDate!
        var conversationUUID: NSUUID = NSUUID()
    }

that are held with another custom object 'Conversation'

    class Conversation {

        var uUID: NSUUID!
        var inboundMessages: [InboundMessage] = []
        var outboundMessages:[OutboundMessage] = []

    }

I'd like to display the conversation ordered by date within a tableview like a normal conversation would be but i cant get my head around how to do it after many attempts. I feel like it would be best to push them all into a sorted array and then let the tableview deal with it but im still not sure how to sort them and then how to put two different custom objects into the same array?

You may have guessed that im very new to swift and iOS so any help would be much appreciated.

Thanks

Wazza
  • 1,725
  • 2
  • 17
  • 49
  • should i say duplicate? http://stackoverflow.com/questions/29902781/sort-an-array-by-nsdates-using-the-sort-function OR http://stackoverflow.com/questions/26577496/how-do-i-sort-a-swift-array-containing-instances-of-nsmanagedobject-subclass-by – Bista Sep 07 '16 at 10:31
  • You think correctly, just put them in the same array and sort by date, but for each object add another property to check if its inbound or outbound, then you can easily place them correctly – Tj3n Sep 07 '16 at 10:32
  • how to put two different custom objects into the same array, you can use array of AnyObject like this [AnyObject] – Arslan Asim Sep 07 '16 at 10:32
  • 1
    @ArslanAsim while this will certainly work it's a non-optimal solution. You're better off using a custom protocol and having each struct implement it, then making the combined array contain members of that protocol. –  Sep 07 '16 at 10:38
  • Thanks fo the quick responses! How can i sort array by date with two custom objects in? i.e how do acces each date within the objects. and then as @Tj3n said how do i add another property to them to check if they are inboun/outbound etc? Ive tried googling these two questions seperately but cant find the answer. Thanks – Wazza Sep 07 '16 at 10:41
  • Is it best to create a new GenericMessage class with, date, inbound = true/false and messageText properties then filter through each inbound/outbound array message and converting it to a GenericMessage and adding it to the Conversation array and then sorting that by date? – Wazza Sep 07 '16 at 10:52
  • Better to make it become one type yes – Tj3n Sep 07 '16 at 11:05

1 Answers1

3

1. Create a base class:

class Message {
    let timeAndDateMessageReceived: NSDate!
    let hasBeenRead: Bool!
    let messageUUID = NSUUID().UUIDString
    let conversationUUID: NSUUID = NSUUID()
}

2. Extend Message class:

class OutboundMessage: Message
class InboundMessage: Message

3. Create a Message array:

var messages: [Message] = inboundMessages as [Message] + outboundMessages as [Message]

4. Order it by timeAndDateMessageReceived:

messages.sort({ $0.timeAndDateMessageReceived.compare($1. timeAndDateMessageReceived) == NSComparisonResult.OrderedAscending })
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49