Swift #28: Foundation
I’m happy to translate the article for you, but I’ll need the full text you’d like translated. Could you please paste the content (excluding the source line you already provided) here? Once I have it, I’ll translate it into Korean while preserving the original formatting and markdown.
일부 Foundation 라이브러리 함수
| 함수 | 설명 |
|---|---|
pow(_:_:) | 기본값이 Double인 밑과 Double인 지수를 사용하여 거듭제곱 연산을 계산합니다. Double을 반환합니다. |
sqrt(_:) | Double 인수의 제곱근을 계산합니다. Double을 반환합니다. |
log(_:) | Double 값의 자연 로그를 계산합니다. log2(_:) 및 log10(_:)와 같은 변형이 있습니다. |
sin(_:) | Double 값의 사인을 반환합니다. 변형: asin(_:), sinh(_:), asinh(_:). |
cos(_:) | Double 값의 코사인을 반환합니다. 변형: acos(_:), cosh(_:), acosh(_:). |
tan(_:) | Double 값의 탄젠트를 반환합니다. 변형: atan(_:), tanh(_:), atanh(_:). |
NSString
import Foundation
let text0: String = "Hola"
let text1: NSString = NSString(string: text0)
print(text1) // "Hola"
let text2: NSString = text0 as NSString
print(text2) // "Hola"
유용한 속성 및 메서드
capitalized: 각 단어의 첫 글자를 대문자로 변환한 문자열을 반환합니다.length: 문자 수를 반환합니다.localizedStringWithFormat(_:_:): 첫 번째 인수(템플릿)와 두 번째 가변 인수에 제공된 값을 사용하여 문자열을 생성합니다.contains(_:): 인수가 원본 문자열에 포함되어 있으면true를 반환합니다.localizedStandardContains(_:): 언어(로케일) 규칙을 고려하여true를 반환합니다.components(separatedBy:): 인수를 구분자로 사용하여 문자열을 분할하고, 결과 부분들을 배열로 반환합니다.replacingOccurrences(of:with:):of:의 모든 발생을with:으로 교체한 새로운 문자열을 반환합니다.trimmingCharacters(in:):in:에 지정된 문자를 문자열의 앞과 뒤에서 제거합니다. 가장 일반적인 인수는whitespaces입니다.
localizedStringWithFormat 예시
let number = 44
let result1 = String.localizedStringWithFormat("Numero: %d", number)
print(result1) // "Numero: 44"
let pi = 3.14159
let lives = 7
let result2 = String.localizedStringWithFormat("Pi: %.2f", pi)
print(result2) // "Pi: 3.14"
let result3 = String.localizedStringWithFormat("Vidas: %.3d", lives)
print(result3) // "Vidas: 007"
Nota:
String구조체는 Unicode 문자를 사용하고,NSString은 UTF‑16 인코딩을 사용합니다.
NSNumber
| 속성 / 메서드 | 설명 |
|---|---|
init(value:) | 지정된 값으로 NSNumber를 생성합니다. |
doubleValue | NSNumber의 Double 값을 가져옵니다. |
intValue | NSNumber의 Int 값을 가져옵니다. |
formatted(_:) | FormatStyle을 사용해 값을 포맷합니다. 숫자에는 IntegerFormatStyle와 FloatingPointFormatStyle이 있습니다. |
FormatStyle
precision(_:)integerAndFractionLength(integerLimits:fractionLimits:)rounded(rule:increment:)grouping(_:)sign(strategy:)decimalSeparator(strategy:).percentcurrency(code:)
사용 예시
let result = (3.14159)
.formatted(
.number
.precision(.fractionLength(2))
)
print(result) // 3.14
Date
날짜를 생성하고 처리하기 위한 여러 클래스가 있습니다:
Date 생성자
-
Date()– 현재 날짜와 시간.let now = Date() -
Date(timeIntervalSinceNow:)– 현재 시점으로부터 초 단위의 오프셋을 적용한 날짜와 시간.let fiveMinutesLater = Date(timeIntervalSinceNow: 5 * 60) -
Date(timeInterval:since:)– 다른 날짜를 기준으로 초 단위의 오프셋을 적용하여 날짜를 생성.let reference = Date() let tenSecondsLater = Date(timeInterval: 10, since: reference) -
Date(timeIntervalSinceReferenceDate:)– 2001년 1월 1일 00:00:00 UTC를 기준으로 초 단위의 오프셋을 적용하여 날짜를 생성.let referenceDate = Date(timeIntervalSinceReferenceDate: 0) // 2001‑01‑01 00:00:00 UTC let oneDayLater = Date(timeIntervalSinceReferenceDate: 86_400) // 1 día después