반응형

스위프트 날짜 구하는 예제를 다뤄보도록 하겠습니당.

 

 //날짜 포맷 (클로저 사용)
    let formatDate:DateFormatter = {
       let f = DateFormatter()
        //날짜는 long 스타일로 출력
        f.dateStyle = .long
        //시간은 short 스타일로 출력
        f.timeStyle = .short
        // 한글로 변경 
        f.locale = Locale(identifier: "Ko_kr")
        return f
    }()
    
    
// 날짜 삽입 (여기서는 테이블 뷰)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        let target = DataManager.shared.memoList[indexPath.row]
        cell.textLabel?.text = target.content
        //문자열로 리턴 해줌.
        cell.detailTextLabel?.text = formatDate.string(for: target.insertDate)

        return cell
    }

결과 : 

결과

 

반응형

+ Recent posts