Swift/Swift
[swift 함수정리] - NSExpression, replacingOccurrences 등
Diana_iOS
2021. 4. 18. 19:13
1. NSExpression
애플 공식 문서: An expression for use in a comparison predicate.
해당 함수는 계산기 등의 연산에 자주 사용되며 문자열을 받아 계산식으로 자동 변환해주는 함수입니다.
class NSExpression : NSObject
checkedWorkingsForPercent에 "6*5-10"의 문자열이 들어갈 경우 expression의 결과로는 (6*5)-10 이 출력됩니다.
2. replacingOccurrences(of:with:)
애플 공식문서 : Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
해당 문자열 중 of에 해당하는 문자가 존재 할 시 with의 문자로 자동 변경합니다. with는 nil일 수 있습니다.
workings 문자열에 "%" 문자열이 존재할 때 이를 "*0.01"문자열로 변경합니다.
3. expressionValue(with:context:)
애플 공식문서: Evaluates an expression using a given object and context.
func expressionValue(with object: Any?, context: NSMutableDictionary?) -> Any?
뒤에 as 등을 사용하여 형변환에 사용할 수 있다.
4. truncatingRemainder(dividingBy:)
애플 공식문서: Returns the remainder of this value divided by the given value using truncating division.
func truncatingRemainder(dividingBy other: Double) -> Double