0
我創造了迅速的字典,它走的是一條很長的時間斯威夫特源文件的問題(20分鐘)編譯編譯與創建字典
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
}
}
我測試過它,我知道它是創建這本字典是因爲我試過刪除一半字典,編譯速度更快。有沒有人有關於如何加快速度的提示?