【发布时间】:2019-12-17 15:24:25
【问题描述】:
我正在尝试消除此警告。我在此行下收到警告:
let removeHashTag = (currentText as? NSString)?.substring(from: 1)
我已经知道它是字符串格式,但是当我尝试输入它时,我不会选择子字符串。
func searchGetTextAutoComplete(_ text: String?) {
var currentText: String
if text == nil {
ViewAnimation.move(tableView, withAlpha: 0.0)
print("textfield is nil")
} else {
if (text?.count ?? 0) == 0 {
ViewAnimation.move(tableView, withAlpha: 0.0)
print("textfield has zero length")
} else {
currentText = text ?? ""
// # included in the beggining of the string
//MARK: you dont needs to downcasted if it already a string
if (text?.count ?? 0) > 1 {
let removeHashTag = (currentText as? NSString)?.substring(from: 1)
SketchManager.shared()?.searchTagsOrUsername(removeHashTag, withCompletion: { success, message, searchResultDic in
if success {
self.followersArray = (searchResultDic?["follower"] as? [String])!
let resultArray = searchResultDic?["result"] as? [Any]
// NSLog(@"%@", resultArray);
if resultArray?.count == 0 {
self.noResultsPlacehoder.isHidden = false
ViewAnimation.move(self.tableView, withAlpha: 0.0)
} else {
self.itemsArray.removeAll()
for dic in resultArray as? [[String : Any]] ?? [] {
self.itemsArray.append(dic)
}
self.tableView.reloadData()
ViewAnimation.move(self.tableView, withAlpha: 1.0)
self.noResultsPlacehoder.isHidden = true
}
}
})
} else {
noResultsPlacehoder.isHidden = false
ViewAnimation.move(tableView, withAlpha: 0.0)
}
}
}
}
【问题讨论】:
-
你为什么要强制转换为
NSString?String有获取子字符串的方法。 -
那是我的想法,但它不会给我那个选项