[英]Compiling Swift source files issue with creating dictionary
I am creating a dictionary in swift and it is taking a very long time (20 min) to compile 我正在快速创建字典,编译时间很长(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
}
}
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? 有人对如何加快速度有一些提示吗?
Assigning a literal value to a dictionary is buggy in the current versions of Swift and likely to trigger compilation issues. 在当前版本的Swift中,将字面值赋给字典是很麻烦的,并且很可能触发编译问题。 A quick workaround is to avoid literals and do it the long way. 一个快速的解决方法是避免使用文字,并且要长期进行。
var dictionaryRepresentation = [String: Any]()
dictionaryRepresentation[key1] = value1
dictionaryRepresentation[key2] = value2
...
It generates more code but it compiles really quickly. 它生成更多代码,但编译速度非常快。
I give a complete code example that compiles very quickly in this manner... I am sure you can modify your own code similarly... 我给出了一个完整的代码示例,它以这种方式可以非常快速地进行编译...我确信您可以类似地修改自己的代码...
import Foundation
func getDict(email : String, zipCode : String, city: String, state: String, country: String, creditRating: String,
wantsPetFriendly: Bool, wantsSmoking: Bool, wantsWasherDryer: Bool, wantsGarage: Bool, wantsDishwasher: Bool,
wantsBackyard: Bool, wantsPool: Bool, wantsGym: Bool, firstName: String, lastName: String,
wantedPayment: NSNumber, id: Int, wantedBedroomCount: NSNumber, wantedBathroomCount: NSNumber,
bio: String?, starRating: Int, maritalStatus: String?, currentOccupation: String?,
withinRangeMiles: Bool, bankruptcies: String, criminalHistory: String?,
driversLicenceNum: Int?, driversLicensePicURL: String?, evictionHistory: String?,
income: Int?, isStudent: Bool?, isVerified: Bool?, previousAddress: String?,
school : String?,studentPhotoIDURL: String?,
studentID: String?, reasonForLeaving: String?) -> [String: Any] {
var dictionaryRepresentation = [String: Any]()
dictionaryRepresentation["kEmail"] = email
dictionaryRepresentation["kZipCode"] = zipCode
dictionaryRepresentation["kCity"] = city
dictionaryRepresentation["kState"] = state
dictionaryRepresentation["kCountry"] = country
dictionaryRepresentation["kCreditRating"] = creditRating
dictionaryRepresentation["kPetsAllowed"] = wantsPetFriendly
dictionaryRepresentation["kSmokingAllowed"] = wantsSmoking
dictionaryRepresentation["kWasherDryer"] = wantsWasherDryer
dictionaryRepresentation["kGarage"] = wantsGarage
dictionaryRepresentation["kDishwasher"] = wantsDishwasher
dictionaryRepresentation["kBackyard"] = wantsBackyard
dictionaryRepresentation["kPool"] = wantsPool
dictionaryRepresentation["kGym"] = wantsGym
dictionaryRepresentation["kFirstName"] = firstName
dictionaryRepresentation["kLastName"] = lastName
dictionaryRepresentation["kMonthlyPayment"] = Int(wantedPayment)
dictionaryRepresentation["kID"] = id
dictionaryRepresentation["kBedroomCount"] = Int(wantedBedroomCount)
dictionaryRepresentation["kBathroomCount"] = wantedBathroomCount
dictionaryRepresentation["kBio"] = bio ?? "No bio available"
dictionaryRepresentation["kStarRating"] = starRating
dictionaryRepresentation["kMaritalStatus"] = maritalStatus ?? "Not specified"
dictionaryRepresentation["kCurrentOccupation"] = currentOccupation ?? "No occupation yet"
dictionaryRepresentation["kWithinRangeMiles"] = withinRangeMiles
dictionaryRepresentation["kBankruptcies"] = bankruptcies
dictionaryRepresentation["kCriminalHistory"] = criminalHistory ?? ""
dictionaryRepresentation["kDriversLicenseNumber"] = driversLicenceNum ?? ""
dictionaryRepresentation["kDriversLicensePicURL"] = driversLicensePicURL ?? ""
dictionaryRepresentation["kEvictionHistory"] = evictionHistory ?? ""
dictionaryRepresentation["kIncome"] = income ?? 0
dictionaryRepresentation["kIsStudent"] = isStudent ?? false
dictionaryRepresentation["kIsVerified"] = isVerified ?? false
dictionaryRepresentation["kPreviousAddress"] = previousAddress ?? ""
dictionaryRepresentation["kReasonsForLeaving"] = reasonForLeaving ?? ""
dictionaryRepresentation["kSchool"] = school ?? ""
dictionaryRepresentation["kStudentID"] = studentID ?? ""
dictionaryRepresentation["kStudentPhotoIdURL"] = studentPhotoIDURL ?? ""
return dictionaryRepresentation;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.