用户可以为我的应用程序提供多个用户选项:
<select name="ORG_ID" #ORG_ID="ngModel" [(ngModel)]="site" class="form-control">
<option *ngFor="let item of this.sharedService.l_operating_units_s"
[ngValue]="item">{{item.NAME}}
</option>
</select>
<!--just for check-->
{{site.NAME}}{{site.BU_ID}}
每个选项都与一个类型相关联。为了建立关联,我做了这个数组:
enum OptionAvailable : String {
case NOTE_GENDER = "note_gender"
case NOTE_AGEMIN = "note_agemin"
case NOTE_AGEMAX = "note_agemax"
case NOTE_ORGA = "note_orga"
case NOTE_DMAX = "note_dmax"
}
然后,当用户在选项菜单中设置一个选项时,我的应用将通过调用一个函数来向我的API发送请求:
static let typeForOption:[OptionAvailable:AnyClass] = [
.NOTE_GENDER : MemberOptions.OptGenderToGrade.self, (ERROR)
.NOTE_AGEMAX : Int.self,
.NOTE_AGEMIN : Int.self,
.NOTE_ORGA : MemberOptions.GradeFrom.self,
.NOTE_DMAX : Int.self
]
问题:我在(错误)处从Swift收到错误:无法将类型“ MemberOptions.OptGenderToGrade.Type”的值转换为预期的字典值类型“ AnyObject.Type”
这是MemberOptions.OptGenderToGrade类:
static func ApiSetOption(
opt:MemberOptions.OptionAvailable,
val:Any,
completionHandler:(()->Void)? = nil,
errorHandler:((Int,String)->Void)? = nil) throws
{
// this is in pseudo code, just to see whats going on :
if val is not of type typeForOption[opt] {
throw WrongTypeError
}
if typeForOption[opt] is not convertible to String {
throw TypeNotStringifiable
}
URLRequest(.POST, "mydomain.com/set/option/\(opt.rawValue)",
withBody:val )
}
答案 0 :(得分:1)
你有
static let typeForOption:[OptionAvailable:AnyClass] = [
AnyClass表示任何类。但是您的类型不是类类型。