| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
- xcode 엔터 표시
- NSTextStorageDelegate
- JPEG
- SwiftUI
- xcode 공백 표시
- JPG
- 팀 개발을 위한 git
- TestFlight
- Tuist
- 테스트 타겟
- nidthirdpartylogin
- webp
- fetchdescriptor
- heic
- 코드스쿼드
- Firestore
- 함께자라기
- Cocoa Pod
- swift 모듈화
- spm 에러
- 클린 아키텍처
- 타뷸레이션
- 캐러셀
- png
- NSTextStorage
- .pbxproj
- contentalignmentpoint
- swiftdata
- 무한스크롤
- github 시작하기
- Today
- Total
Sure, Why not?
SwiftData) FetchDescriptor 에러 본문
Cannot convert value of type 'PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<StockModel>, UUID>, PredicateExpressions.KeyPath<PredicateExpressions.Value<Stock>, UUID>>' to closure result type 'any StandardPredicateExpression<Bool>'

SwiftData Predicate 내부에서 외부 변수 참조 시 발생되는 에러
사진과 같이 직접 외부 변수를 참조하니 컴파일 에러가 발생하였습니다.
SwiftData의 Predicate는 쿼리로 변환되어야 하기 때문에, 컴파일 시점에 모든 조건이 정적으로 분석이 되어야합니다.
그래서 외부 변수를 직접 참조하면 런타임에 값이 변경될 수 있기 때문에, 컴파일 타임에 에러가 발생되었습니다.
해결방법
그래서 외부 변수를 외부에 선언해서 클로저에서 캡처된 변수를 사용하도록 해결하였습니다.
let stockId = stock.id
let descriptor = FetchDescriptor<StockModel>(
predicate: #Predicate<StockModel> { $0.id == stockId }
)
Reference
Cannot convert value of type to cl… | Apple Developer Forums
As read here, you cannot reference a model object inside of a Predicate. So if you wanted to filter for a User model, you could by using its id outside of the Predicate like such: let userId = uder.id #Predicate { tran in tran.tranInitiator.id == userId }
forums.developer.apple.com
'💻' 카테고리의 다른 글
| 콜백 방식의 비동기 코드를 async/await으로 사용하기 (0) | 2025.02.01 |
|---|---|
| UIScrollView 콘텐츠 오른쪽 정렬하기 (1) | 2024.12.22 |
| Tuist 기존 프로젝트에 테스트 타겟 추가 (2) | 2024.12.06 |
| Xcode16.1) 소스트리 사용시 SPM 에러 (0) | 2024.11.03 |
| Runtime linking failure (1) | 2024.10.08 |