我正在使用swift进行应用程序开发并使用swift lint.但我收到有关以下代码的警告:
for settingsKeys in searchResults {
if settingsKeys.key == settingsObject.key {
settingsKeys.value = settingsObject.value
try context.save()
}
}
Run Code Online (Sandbox Code Playgroud)
屏幕截图如下:
我有一个像这样的POST主体参数:
{
"id": 0,
"name": "string",
"contactInfo": "string",
"message": "string"
}
Run Code Online (Sandbox Code Playgroud)
所以,因为我正在使用Alamofire发布参数,我正在描述这样的帖子体字典:
let body = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message]
class func postUserFeedback(userID: Int, userName: String, contactInfo: String, message: String,completionHandler: @escaping (FeedbackResponse?) -> Void) {
let body = ["id": userID, "name": userName, "contactInfo": contactInfo, "message": message]
request(route: .userFeedback, body: body).responseObject { (response: DataResponse<FeedbackResponse>) in
response.result.ifSuccess({
completionHandler(response.result.value)
})
response.result.ifFailure {
completionHandler(nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这个语法中做错了什么?
我在swift3中有以下代码,我使用swift lint来代码.代码如下:
func selectedMenuInLoggedOutState(sender: UIButton) {
switch sender.tag {
case 1:
if let menu = LeftGuestMenu(rawValue: 0) {
self.changeGuestViewController(menu)
}
case 2:
if let menu = LeftGuestMenu(rawValue: 1) {
self.changeGuestViewController(menu)
}
case 3:
if let menu = LeftGuestMenu(rawValue: 2) {
self.changeGuestViewController(menu)
}
case 4:
if let menu = LeftGuestMenu(rawValue: 3) {
self.changeGuestViewController(menu)
}
case 5:
if let menu = LeftGuestMenu(rawValue: 4) {
self.changeGuestViewController(menu)
}
case 6:
if let menu = LeftGuestMenu(rawValue: 5) {
self.changeGuestViewController(menu)
}
default:
break
}
}
Run Code Online (Sandbox Code Playgroud)
swiftlint会产生一个称为圈复杂度的警告.为什么会出现此警告警告以及如何解决此问题?
我使用以下代码提供 UILabel 文本之间的行距。
let daysAttrString = NSMutableAttributedString(string: allDays)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 7
paragraphStyle.minimumLineHeight = 7
daysAttrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, daysAttrString.length))
cell.operationalDays.attributedText = daysAttrString
Run Code Online (Sandbox Code Playgroud)
但是我在使用 SwiftLint 时收到以下警告,如下所示:
为什么显示它以及如何解决这个问题?
这是我的以下代码:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont().robotoMedium(withFontSize: 10)
headerLabel.textColor = CustomColor.lightGrey.color
headerLabel.text = "Travel Shops"
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
headerLabel.translatesAutoresizingMaskIntoConstraints = false
headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0))
headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: tableView, attribute: .leading, multiplier: 1, constant: 0))
headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, …Run Code Online (Sandbox Code Playgroud) 我正在尝试自定义UISegmented控件的字体,我使用以下代码,如下所示:
let selectedfont = UIFont(name:"Roboto-Bold", size:22)
let unselectedfont = UIFont(name:"Roboto-Regular", size:22)
let normalTextAttributes: [String: Any] = [
NSForegroundColorAttributeName: UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha: 1.0),
NSFontAttributeName: unselectedfont as Any
]
let selectedTextAttributes: [String: Any] = [
NSForegroundColorAttributeName: UIColor.black,
NSFontAttributeName: selectedfont as Any
]
self.setTitleTextAttributes(normalTextAttributes, for: UIControlState())
self.setTitleTextAttributes(selectedTextAttributes, for: .highlighted)
self.setTitleTextAttributes(selectedTextAttributes, for: .selected)
Run Code Online (Sandbox Code Playgroud)
但应用程序崩溃显示: 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [NSNull pointSize]:无法识别的选择器
这是什么问题?任何帮助都会得到满足.
我从webservice获得了一个特殊的双号,比如来自webservice的0.097或0.034.因此,如果我得到一个特定的数字,如0.56或0.5,我需要在0.56中加零,在0.5中加两个零.如何在swift3中执行此操作?目前我在做:
class func appendString(data: Int) -> String {
let value = data
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 3 // for float
formatter.maximumFractionDigits = 3 // for float
formatter.minimumIntegerDigits = 1
formatter.paddingPosition = .afterPrefix
formatter.paddingCharacter = "0"
return formatter.string(from: NSNumber(floatLiteral: Double(value)))!
}
Run Code Online (Sandbox Code Playgroud)
任何想法如何实现上述告诉逻辑?