简体   繁体   English

从 String 到 NSString 的条件转换总是成功

[英]Conditional cast from String to NSString always succeeds

I'm trying to eliminate this warning.我正在尝试消除此警告。 I get the warning under this line:我在这一行下收到警告:

let removeHashTag = (currentText as? NSString)?.substring(from: 1)

I know it already in a string format, and but when I tried typing it won't give me the option to substring.我知道它已经是字符串格式,但是当我尝试输入时,它不会给我子字符串的选项。

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)
            }
        }
    }
}

While you are using constant value for substring from 1 you can easily use当您为 1 的子字符串使用常量值时,您可以轻松使用

let removedHashTag = currentText.dropFirst()
let editedString = String(removedHashTag)

Remove all the question marks.去掉所有的问号。

(currentText as NSString).substring(from: 1)

Even better, don't cast to NSString at all.更好的是,根本不要转换为 NSString。 Swift String gives you the same powers: Swift String 给你同样的权力:

currentText.suffix(currentText.count-1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从“UIButton”到“UIButton”的条件转换总是成功 - Conditional cast from 'UIButton' to 'UIButton' always succeeds 从 'AFError' 到 'AFError' 的条件转换总是成功 - Conditional cast from 'AFError' to 'AFError' always succeeds 黄色警告:从UITextDocumentProxy到UIKeyInput的条件强制转换始终成功 - yellow Warnings : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds 从“AppDelegate”到“UNUserNotificationCenterDelegate”的条件转换总是成功(颤振) - Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds (Flutter) 黄色警告:从“AVCaptureVideoPreviewLayer”到“AVCaptureVideoPreviewLayer”的条件转换总是成功 - Yellow warning: Conditional cast from 'AVCaptureVideoPreviewLayer' to 'AVCaptureVideoPreviewLayer' always succeeds 如何删除警告“从'Any'到'AnyObject'的有条件强制转换始终成功” - How to remove warning “Conditional cast from 'Any' to 'AnyObject' always succeeds” 从'String'到'String'的条件向下转换总是成功 - Swift Error - Conditional downcast from 'String' to 'String' always succeeds - Swift Error 从字符串到字符串的有条件强制转换总是迅速完成 - Conditional cast from string to string always succeds in swift 3 Swift:确定从字符串转换为整数是否成功 - Swift: Determining if cast to integer from string succeeds 从PFFile到PFFile的条件转换始终成功 - Conditional cast from PFFile toPFFile always succeed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM