The argument type "Int" does not match the expected type "NSCoding & NSCopying & NSObjectProtocol"

I am new to Swift and have tried some lessons to learn and polish my knowledge in Swift. I came across an error above in this code that I did not understand. If any of you have an idea, please explain what is wrong here.

    let textChoices =   [
    ORKTextChoice(text: "Create a ResearchKit app", value:0),
    ORKTextChoice(text: "Seek the Holy grail", value:1),
    ORKTextChoice(text: "Find a shrubbery", value:2)
]

I solved the error by suggesting Xcode and now my code looks like

    let textChoices =   [
    ORKTextChoice(text: "Create a ResearchKit app", value:0 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "Seek the Holy grail", value:1 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "Find a shrubbery", value:2 as NSCoding & NSCopying & NSObjectProtocol)
]

There is another solution that I got from answer . Although it works, I still do not quite understand the problem and solution. What concept is missing.

+4
source share
1 answer

ORKTextChoice value:, Swift , Int - NSCoding, NSCopying NSObjectProtocol, Objective-C, NSNumber.

, NSCoding & NSCopying & NSObjectProtocol, NSNumber ( ), :

let textChoices = [
    ORKTextChoice(text: "Create a ResearchKit app", value: 0 as NSNumber),
    ORKTextChoice(text: "Seek the Holy grail", value: 1 as NSNumber),
    ORKTextChoice(text: "Find a shrubbery", value: 2 as NSNumber)
]

Swift 3, Swift Objective-C. , SE-0072: Swift, . as.

+5

Source: https://habr.com/ru/post/1657046/


All Articles