我在swift中创建一个字典,需要很长时间(20分钟)来编译
[RegularExpression("[A-Za-z]*", ErrorMessage = "Use Alphabets Only")]
我已经测试了它,我知道这是这本字典的创建,因为我已经尝试删除了一半的字典,它的编译速度要快得多。有没有人对如何提高速度有一些提示?
答案 0 :(得分:1)
在字典中为字典分配文字值是当前Swift版本中的错误,可能会触发编译问题。一个快速的解决方法是避免文字,并做很长的路。
var dictionaryRepresentation = [String: Any]()
dictionaryRepresentation[key1] = value1
dictionaryRepresentation[key2] = value2
...
它生成更多代码,但编译速度非常快。
我提供了一个完整的代码示例,以这种方式快速编译...我相信你可以同样修改自己的代码......
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;
}