ios - 如何检查 NSArray 中的 NSDate yyyy/mm/dd 并进行比较?

标签 ios swift sorting nsarray nsdate

我有一个 NSArray,里面有一个 NSDate 类型的 var 给出这种格式“timeStamp =”2015-08-18 16:58:31”

我只想比较所有数组中的日期 2015-08-18

比较它,如果相同的日期只有第一个显示完整的 NSDate

其余具有相同日期的仅显示用户界面上的时间

这是我到目前为止所做的:

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
}

屏幕截图 http://i.stack.imgur.com/wcsSz.jpg

最佳答案

经过一番研究,我做了如下解决方案

首先这个扩展非常好,我的方法取决于它 https://stackoverflow.com/a/27369380/5188737

如果有人有更好的解决方案或清洁 如果您能将其发布在这里,我将不胜感激

很抱歉没有评论

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
}

关于ios - 如何检查 NSArray 中的 NSDate yyyy/mm/dd 并进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32091213/

相关文章:

ios - 我的 UITableViewCell 正在重用以前的单元格 UIImage

ios - Swift NSPredicate 不在

Javascript——按照指定的对象属性顺序对对象数组进行排序

iOS - Objective C - 无法使用 UInt8 渲染像素

ios - 为什么在 UIScrollView 中看不到 UIStackView?

ios - 在某些 Controller 中禁用横向时,横向启动应用程序会扭曲整个应用程序

c++ - 仅读取存储在 vector 中的字符串中的第一个字符

ios - Firebase While 循环 Swift 4

ios - 在 Swift 中子类化 View Controller

javascript - 获取属于 JavaScript/Node js 中特定 ID 的数组对象的计数