I am creating a dictionary in swift and it is taking a very long time (20 min) to compile
import Foundation
extension Renter {
var dictionaryRepresentation: [String: Any]? {
guard let email = email,
let zipCode = wantedZipCode,
let city = wantedCity,
let state = wantedState,
let country = wantedCountry,
let creditRating = creditRating,
let firstName = firstName,
let lastName = lastName,
let id = id
else { return nil }
var dictionaryRepresentation: [String: Any] = [UserController.kEmail: email,
UserController.kZipCode: zipCode,
UserController.kCity: city,
UserController.kState: state,
UserController.kCountry: country,
UserController.kCreditRating: creditRating,
UserController.kPetsAllowed: wantsPetFriendly,
UserController.kSmokingAllowed: wantsSmoking,
UserController.kWasherDryer: wantsWasherDryer,
UserController.kGarage: wantsGarage,
UserController.kDishwasher: wantsDishwasher,
UserController.kBackyard: wantsBackyard,
UserController.kPool: wantsPool,
UserController.kGym: wantsGym,
UserController.kFirstName: firstName,
UserController.kLastName: lastName,
UserController.kMonthlyPayment: Int(wantedPayment),
UserController.kID: id,
UserController.kBedroomCount: Int(wantedBedroomCount),
UserController.kBathroomCount: wantedBathroomCount,
UserController.kBio: bio ?? "No bio available",
UserController.kStarRating: starRating,
UserController.kMaritalStatus: maritalStatus ?? "Not specified",
UserController.kCurrentOccupation: currentOccupation ?? "No occupation yet",
UserController.kWithinRangeMiles: withinRangeMiles,
UserController.kBankruptcies: bankruptcies,
UserController.kCriminalHistory: criminalHistory ?? "",
UserController.kDriversLicenseNumber: driversLicenceNum ?? "",
UserController.kDriversLicensePicURL: driversLicensePicURL ?? "",
UserController.kEvictionHistory: evictionHistory ?? "",
UserController.kIncome: income ?? 0,
UserController.kIsStudent: isStudent ?? false,
UserController.kIsVerified: isVerified ?? false,
UserController.kPreviousAddress: previousAddress ?? "",
UserController.kReasonsForLeaving: reasonForLeaving ?? "",
UserController.kSchool: school ?? "",
UserController.kStudentID: studentID ?? "",
UserController.kStudentPhotoIdURL: studentPhotoIDURL ?? ""]
guard let profileImageArray = self.profileImages?.array as? [ProfileImage] else { return dictionaryRepresentation }
let imageURLs = profileImageArray.flatMap({$0.imageURL})
dictionaryRepresentation[UserController.kImageURLS] = imageURLs
guard let occupationHistory = self.occupation?.allObjects as? [Occupation] else { return dictionaryRepresentation }
let occupationDicts = occupationHistory.flatMap({ $0.dictionaryRepresentation })
dictionaryRepresentation[UserController.kOccupationHistory] = occupationDicts
return dictionaryRepresentation
}
}
I've tested it and I know it is the creation of this dictionary because I've tried removing half of the dictionary and it compiles much faster. Does anybody have some tips on how to speed this up?