"%를 사용할 수 없음 : 대신 truncatingRemainder 사용"은 무엇을 의미합니까?
확장 코드를 사용할 때 다음 오류가 발생합니다. 다른 연산자를 사용하거나 인터넷 검색을 기반으로 표현식의 값을 수정하도록 요청하는지 확실하지 않습니다.
오류 : %를 사용할 수 없음 : 대신 truncatingRemainder를 사용하십시오.
확장 코드 :
extension CMTime {
var durationText:String {
let totalSeconds = CMTimeGetSeconds(self)
let hours:Int = Int(totalSeconds / 3600)
let minutes:Int = Int(totalSeconds % 3600 / 60)
let seconds:Int = Int(totalSeconds % 60)
if hours > 0 {
return String(format: "%i:%02i:%02i", hours, minutes, seconds)
} else {
return String(format: "%02i:%02i", minutes, seconds)
}
}
}
분 및 초 변수를 설정할 때 오류가 발생합니다.
CMTimeGetSeconds()
부동 소수점 숫자 ( Float64
일명 Double
)를 반환합니다 . Swift 2에서는 부동 소수점 나누기의 나머지를 다음과 같이 계산할 수 있습니다.
let rem = 2.5 % 1.1
print(rem) // 0.3
Swift 3에서는 다음과 같이 수행됩니다.
let rem = 2.5.truncatingRemainder(dividingBy: 1.1)
print(rem) // 0.3
코드에 적용 :
let totalSeconds = CMTimeGetSeconds(self)
let hours = Int(totalSeconds / 3600)
let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60)
let seconds = Int(totalSeconds.truncatingRemainder(dividingBy: 60))
그러나이 특별한 경우에는 처음에 기간을 정수로 변환하는 것이 더 쉽습니다.
let totalSeconds = Int(CMTimeGetSeconds(self)) // Truncate to integer
// Or:
let totalSeconds = lrint(CMTimeGetSeconds(self)) // Round to nearest integer
그런 다음 다음 줄은
let hours = totalSeconds / 3600
let minutes = (totalSeconds % 3600) / 60
let seconds = totalSeconds % 60
%
나머지 연산자은 정수 타입에 대해 정의된다. 부동 소수점 유형의 경우 원하는 IEEE 754 분할 / 나머지 동작의 종류에 대해 더 구체적이어야하므로 remainder
또는 메서드를 호출해야합니다 truncatingRemainder
. (부동 소수점 수학을 수행하는 경우 실제로 이것과 다른 많은 것들을 신경 써야 합니다. 그렇지 않으면 예상치 못한 / 나쁜 결과를 얻을 수 있습니다.)
실제로 정수 모듈러스를 수행하려면을 CMTimeGetSeconds
사용하기 전에 의 반환 값을 정수 로 변환해야합니다 %
. (그렇게한다면 CMTime
중요 할 수있는 사용 위치에 따라 분수 초를 잘라낼 것 입니다. 예를 들어 분 : 초 : 프레임을 원하십니까?)
CMTime
UI에서 값을 표시 하려는 방법에 따라 초 값을 추출하여 전달 NSDateFormatter
하거나 NSDateComponentsFormatter
적절한 로케일 지원을받는 것이 더 나을 수 있습니다 .
신속한 3에서 간단한 모듈로 구문을 다시 가져옵니다.
이 구문은 실제로 Apple의 공식 빠른 메일 링리스트 에서 제안 되었지만 어떤 이유로 덜 우아한 구문을 선택했습니다.
infix operator %%/*<--infix operator is required for custom infix char combos*/
/**
* Brings back simple modulo syntax (was removed in swift 3)
* Calculates the remainder of expression1 divided by expression2
* The sign of the modulo result matches the sign of the dividend (the first number). For example, -4 % 3 and -4 % -3 both evaluate to -1
* EXAMPLE:
* print(12 %% 5) // 2
* print(4.3 %% 2.1) // 0.0999999999999996
* print(4 %% 4) // 0
* NOTE: The first print returns 2, rather than 12/5 or 2.4, because the modulo (%) operator returns only the remainder. The second trace returns 0.0999999999999996 instead of the expected 0.1 because of the limitations of floating-point accuracy in binary computing.
* NOTE: Int's can still use single %
* NOTE: there is also .remainder which supports returning negatives as oppose to truncatingRemainder (aka the old %) which returns only positive.
*/
public func %% (left:CGFloat, right:CGFloat) -> CGFloat {
return left.truncatingRemainder(dividingBy: right)
}
이 간단한 신속한 3 마이그레이션 팁은 많은 통찰력 (35k loc / 8 일 마이그레이션)이 포함 된보다 포괄적 인 빠른 3 마이그레이션 가이드의 일부입니다. http://eon.codes/blog/2017/01/12/swift-3-migration /
다음은 Swift 3에서 작동하는 것으로 나타났습니다.
let minutes = Int(floor(totalSeconds / 60))
let seconds = Int(totalSeconds) % 60
( ) totalSeconds
는 어디에 있습니까 ?TimeInterval
Double
'program story' 카테고리의 다른 글
OSX 10.10 yosemite beta on git pull : git-sh-setup : No such file or directory (0) | 2020.09.05 |
---|---|
사용 설명 누락으로 인해 앱이 거부 됨 (Xcode8) (0) | 2020.09.05 |
adb 명령을 사용하여 SD 카드없이 기기에 파일을 푸시하는 방법 (0) | 2020.09.05 |
Visual Studio 2017 (.NET Core)의 자동 버전 관리 (0) | 2020.09.05 |
Or 대 OrElse (0) | 2020.09.05 |