0

I have an NSArray, inside I have a var of kind NSDate which give this format "timeStamp = "2015-08-18 16:58:31"

I want to compare in all the array the date only 2015-08-18

compare it and if same date only the first one show full NSDate

and the rest with same date show only the time on UI

This is what I did so far:

func getAllMessages() -> NSArray{
    var allMessages = self.mutableSetValueForKey("messages").allObjects as NSArray
    let timeStampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: true)

    var sortByTime = allMessages.sortedArrayUsingDescriptors([timeStampSortDescriptor])
    println("\(sortByTime)")
    return sortByTime
}

screen shot https://i.stack.imgur.com/wcsSz.jpg

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

After some research i did made a solution as following

First this extension is very good and my way is depends on it https://stackoverflow.com/a/27369380/5188737

If anyone have better solution or clean I will appreciate if you can post it here

I'm sorry there's no comments

func loadMessages(){

    if chatRoom!.messages.count > 0{
        var i = 0
        var tempMsgDate:NSDate?
        var chatMessage:ChatMessage?

        for message in chatRoom!.getAllMessages(){
            let msg = message as! Message
            if i == 0{
                tempMsgDate = msg.timeStamp
                chatMessage = ChatMessage(incoming: msg.income, text: msg.message, sentDate: convertNSDateToString(msg.timeStamp))
                i++
            }else{
                //if the tempMsgDate (which is the first of the same
                //date in the nsarray) 
                if checkIfSameDayDate(tempMsgDate!,date2: msg.timeStamp){
                    var tempDate = msg.timeStamp.time
                    let dateFormatter = NSDateFormatter()
                    dateFormatter.dateFormat = "HH:mm"
                    let date = dateFormatter.dateFromString(tempDate)
                    println("loadmessages method:  \(date?.time)")
                    chatMessage = ChatMessage(incoming: msg.income, text: msg.message, sentDate: msg.timeStamp.time)
                }else{//after he got to different date it save it as
                      //first of same date to follow 
                    tempMsgDate = msg.timeStamp
                    chatMessage = ChatMessage(incoming: msg.income, text: msg.message, sentDate: convertNSDateToString(msg.timeStamp))
                }
            }

            var msgCollection:[ChatMessage] = [ChatMessage]()
            msgCollection.append(chatMessage!)
            chat.loadedMessages.append(msgCollection)
        }
    }
}


func convertNSDateToString(date:NSDate) -> String{
    let dateString = date.date + " " + date.time
    println("in convert: \(dateString)")
    return dateString
}

func checkIfSameDateFromSendMSGAndReciveMSG(date:NSDate) -> Bool{
    for message in chatRoom!.getAllMessages(){
        let msg = message as! Message
        if msg.timeStamp.date == date.date{
            return true
        }
    }
    return false
}
//checks if the date1 equal to date2    
func checkIfSameDayDate(date1:NSDate,date2:NSDate) -> Bool{
    if date1.date == date2.date{
        return true
    }
    return false
}
Community
  • 1
  • 1