2016-10-02 3 views
-1

엔티티에 올바른 유형은 무엇입니까?Xcode 8 (신속한 3) 엔티티의 유형은 무엇입니까?

예를 들어 값을 엔티티의 속성에 저장하는 경우 아래의 세 가지 물음표를 어떤 유형 선언으로 대체해야합니까?

func setSample() { 
    let saveSample: <type???> = SampleEntity(context: context) 
    saveSample.sampleAttribute = "Save a new string." 
    do { 
     try context.save() 
    } catch { 
     print("Error with save: \(error)") 
    } 
} 

FWIW : 내가 원하는

Cannot convert value of type 'SampleEntity' to specified type 'NSEntityDescription' 
Value of type 'NSEntityDescription' has no member 'sampleAttribute' 

상황에 이런 식으로 뭔가 할 수있는 변수로 개체를 만들 :

func setSample() { 
    var saveSample: ??? 
    if (criteria) { saveSample = Entity1(context: context) } 
    if (criteria) { saveSample = Entity2(context: context) } 
    if (criteria) { saveSample = Entity3(context: context) } 
    saveSample.sampleAttribute1 = "Foo" 
    saveSample.sampleAttribute2 = "Bar" 
    saveSample.sampleAttributeNth = "Etc" 
    do { 
     try context.save() 
    } catch { 
     print("Error with save: \(error)") 
    } 
} 
+0

우리는 당신이 성취하고자하는 것에 대해 더 많은 맥락과 'Entity1'과'Entity2'의 선언이 무엇인지를 필요로합니다. – Alexander

+0

@ Alexander Momchliov, 'Entity1','Entity2', 'Entity3'은 CoreData.xcdatamodeld에 나열된 엔티티 이름입니다. –

답변

0

사용 ??? = SampleEntity를하거나 제거 NSEntityDescription는 오류를 반환 ??? 부분 완전하게 :

let saveSample = SampleEntity(context: context) 
let saveSample: SampleEntity = SampleEntity(context: context) 
let saveSample: SampleEntity 
+0

후자의 접근 방식에서는 작동하지 않습니다.이 방법에서는 몇 가지 기준에 따라 나중에 여러 옵션 중 하나를 사용하여 변수를 채우도록 선언합니다. – Alexander

+0

확실히 맞습니다. 수정 된 답변 – shallowThought