Mahmut Akyol Mahmut Akyol - 1 month ago 8
Swift Question

Swift 3 [String: AnyObject] Expression was too complex Error

func createAlmsBoxParams(_ benefactorModel: BenefactorInformationsModel) -> [String: AnyObject]{
let general = General()
let array = [
"UserID": UserDefaults.standard.string(forKey: "userID")! as AnyObject,
"BenefactorMembersNo": benefactorModel.benefactorMembersNo as AnyObject,
"Gender": "true" as AnyObject,
"Firstname": benefactorModel.firstname as AnyObject,
"Lastname": benefactorModel.lastname as AnyObject,
"Alias": "Alias" as AnyObject,
"IdentityNo": benefactorModel.identityNo as AnyObject,
"PhoneNumber": benefactorModel.phoneNumber as AnyObject,
"PhoneRegionCode": benefactorModel.phoneRegionCode as AnyObject,
"PhoneCountryCode": benefactorModel.phoneCountryCode as AnyObject,
"Email": benefactorModel.email as AnyObject,
"Country": benefactorModel.country as AnyObject,
"City": benefactorModel.city as AnyObject,
"District": benefactorModel.district as AnyObject,
"RefDescription": benefactorModel.refDescription as AnyObject,
"Address": benefactorModel.address as AnyObject,
"EntityID": UserDefaults.standard.string(forKey: "entityID")! as AnyObject,
"EntityLevel": benefactorModel.entityLevel as AnyObject,
"DateOfBirth": "19700101" as AnyObject,
"Title": "Title" as AnyObject,
"Division": general.UniqueID() as AnyObject,
"Job": benefactorModel.job as AnyObject,
"IsBusiness": benefactorModel.isBusiness as AnyObject,
"Company": "Company" as AnyObject,
"IsMarried": benefactorModel.isMarried as AnyObject,
"Girl": 0 as AnyObject,
"Boy": 0 as AnyObject,
"Education": 28 as AnyObject,
"How_To_Contact_Us": 29 as AnyObject,
"Member_Type": 30 as AnyObject,
"Member_Grup": "sample string 31" as AnyObject,
"Member_Rank": 32 as AnyObject,
"Coordinate": benefactorModel.coordinate as AnyObject,
"Locked": true as AnyObject,
"Canceled": true as AnyObject,
"Deleted": true as AnyObject,
"IsHaveAlmsBox": true as AnyObject,
"fromWhere": 2 as AnyObject
]
return array as [String: AnyObject]
}


How can I solve this problem in this array ? I tried many method

What I tried ->


let array: [String: AnyObject] = ["": ... as String(or Int, Double ect.)]...

func createAlmsBoxParams() -> [String: Any] ...

I deleted "as AnyObject" near by all parameters ...


but didn't solve

Answer

First, it's not really an array, it's a dictionary.

Second, swift is having some difficulties handling huge literals like this. What you can do is define a var dictionary and put entries one by one:

var dictionary =  [String: Any]()
dictionary["key1"] = value1
dictionary["key2"] = value2
//...