RSDCodableChoiceInputFieldObject
public final class RSDCodableChoiceInputFieldObject<T> : RSDInputFieldObject, RSDChoiceOptions where T : Decodable, T : Encodable
RSDCodableChoiceInputFieldObject
is a concrete implementation of RSDChoiceInputField
that subclasses
RSDInputFieldObject
to include a list of choices for a multiple choice or single choice input field.
It is designed to be used by RSDFactory
or a subclass to encode and decode the choices as a typed array
of RSDChoiceObject
objects.
-
A list of choices for the input field.
Declaration
Swift
public private(set) var choices: [RSDChoice] { get }
-
The default answer associated with this option set.
Declaration
Swift
public private(set) var defaultAnswer: Any? { get }
-
Override
isOptional
to allow fornil
behavior if there is only one choice. Otherwise, there isn’t really a way for the user to not select that choice.Declaration
Swift
override public var isOptional: Bool { get set }
-
This is a required initializer for copying, but the choices will be an empty array.
Declaration
Swift
public required init(identifier: String, dataType: RSDFormDataType)
-
Declaration
Swift
override public func copyInto(_ copy: RSDInputFieldObject)
-
Initialize from a
Decoder
. This method uses theRSDFormDataType.BaseType
associated with this input field to decode a list ofRSDChoiceObject
objects with the appropriateValue
type.example:
// A JSON example where this is for a single choice input field that is a `decimal` base type. // This will decode the choices as an array of `RSDChoiceObject<Double>` objects. let json = """ { "identifier": "foo", "prompt": "Choose a number", "type": "singleChoice.decimal", "uiHint": "picker", "choices" : [{ "value" : 0, "text" : "0"}, { "value" : 1.2, "text" : "1.2"}, { "value" : 3.1425, "text" : "pi", "detail" : "Is the magic number" }, { "text" : "None of the above", "isExclusive" : true }], } """.data(using: .utf8)! // our data in native (JSON) format // A multiple choice question where the choices are coded as a list of text strings. This will // decode the choices as an array of `RSDChoiceObject<String>` objects where `value == text`. let json = """ { "identifier": "step3", "title": "Step 3", "type": "multipleChoice", "choices" : ["alpha", "beta", "charlie", "delta"] } """.data(using: .utf8)! // our data in native (JSON) format // A single choice question where each choice has an icon image representing the choice. This will // decode the choices as an array of `RSDChoiceObject<Int>` objects. let json = """ { "identifier": "happiness", "title": "How happy are you?", "type": "singleChoice.integer", "defaultAnswer": 3, "choices": [{ "text": "delighted", "detail": "Nothing could be better!", "value": 1, "icon": "moodScale1" }, { "text": "good", "detail": "Life is good.", "value": 2, "icon": "moodScale2" }, { "text": "so-so", "detail": "Things are okay, I guess.", "value": 3, "icon": "moodScale3" }, { "text": "sad", "detail": "I'm feeling a bit down.", "value": 4, "icon": "moodScale4" }, { "text": "miserable", "detail": "I cry into my pillow every night.", "value": 5, "icon": "moodScale5" }] } """.data(using: .utf8)! // our data in native (JSON) format // A JSON example where this is for a single choice input field that is a `bool` base type. This will // decode the choices as an array of `RSDChoiceObject<Bool>` objects. let json = """ { "identifier": "heightLimit", "prompt": "Are you tall?", "type": "singleChoice.boolean", "choices" : [{ "value" : true, "text" : "Yes"}, { "value" : false, "text" : "No"}, { "text" : "I don't know", "isExclusive" : true }], } """.data(using: .utf8)! // our data in native (JSON) format
Throws
DecodingError
if there is a decoding error.Declaration
Swift
public required init(from decoder: Decoder) throws
Parameters
decoder
The decoder to use to decode this instance.
-
Encode the result to the given encoder.
Throws
EncodingError
Declaration
Swift
override public func encode(to encoder: Encoder) throws
Parameters
encoder
The encoder to use to encode this instance.