[iOS] Formatted가 뭐야

Madeline👩🏻‍💻·2024년 1월 3일
0

iOS study

목록 보기
29/50
post-thumbnail

iOS 15부터 새로운 Formatted API가 등장했따!
숫자, 날짜, 시간 등의 데이터를 사용자가 사용하는 현지화된 문자열로 변환해준다.

Date

func dateFormatStyle() {
	let value = Date()
        
	print(value)
	// 2024-01-03 01:47:54 +0000
    
	print(value.formatted())
	// 1/3/2024, 10:47 AM
    
	print(value.formatted(date: .complete, time: .omitted))
	// Wednesday, January 3, 2024
    
	print(value.formatted(date: .abbreviated, time: .shortened))
	// Jan 3, 2024 at 10:47 AM
        
	let locale = Locale(identifier: "ko-KR")
        
	let result = value.formatted(.dateTime.locale(locale).day().month(.twoDigits).year())
	print(result)
	// 2024. 01. 3.
        
	let result2 = value.formatted(.dateTime.day().month().year())
	print(result2)
	// Jan 3, 2024
}

애플 공식 문서의 예시는 아래와 같다.

let birthday = Date()


birthday.formatted(date: .complete, time: .omitted) // Sunday, January 17, 2021
birthday.formatted(date: .omitted, time: .complete) // 4:03:12 PM CST

let birthday = Date()


birthday.formatted(date: .long, time: .shortened) // January 17, 2021, 4:03 PM
birthday.formatted(date: .abbreviated, time: .standard) // Jan 17, 2021, 4:03:12 PM
birthday.formatted(date: .numeric, time: .complete) // 1/17/2021, 4:03:12 PM CST


birthday.formatted() // Jan 17, 2021, 4:03 PM

기본 날짜 스타일은 abbreviated이고,
기본 시간 스타일은 shortened이다.

Number, String

여기도 엄청 많다

func numberFormatStyle() {
	print(50.formatted(.percent))
    // 50%
    
	print(123456789.formatted())
    // 123,456,789
        
	print(123456789.formatted(.currency(code: "krw")))
    // ₩123,456,789
        
	let result = ["올라프", "빵빵이", "포키"].formatted()
	print(result)
    // 올라프, 빵빵이, and 포키
}
profile
Major interest in iOS 🍀 & 🍎

0개의 댓글