【问题标题】:Capitalize start of certain words in optional string将可选字符串中某些单词的开头大写
【发布时间】:2023-03-08 16:30:02
【问题描述】:

我使用 Parse 作为我的应用程序的后端。 我目前正在处理寄存器视图,我遇到了这个问题。 这是我的代码:

user.signUpInBackgroundWithBlock { 
        (succeeded: Bool!, error: NSError!) -> Void in 
        if error == nil { 
            // Hooray! Let them use the app now. 
        } else {
            var errorCode = error.userInfo!["error"] as NSString
            var alertController = UIAlertController(title: "Error", message: errorCode, preferredStyle: .Alert)
            var okButton = UIAlertAction(title: "Ok", style: .Default, handler: nil) 
            alertController.addAction(okButton) 
            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }

好的,如您所见,如果出现问题,将创建一个 NSError 并将其存储在变量 error 中。现在默认情况下,存储在那里的任何错误都是小写。

当我不知道它会是什么时,我如何将字符串的开头大写。 例如,当前字符串将类似于 '用户名 bob2211 已被占用' 我在寻找什么 '用户名 bob2211 已被占用。'

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    您需要获取第一个字符,将其大写,然后将第一个字符替换为新的大写字符。我认为这应该很接近,

            var errorCode = error.userInfo!["error"] as NSString
            var firstLetter = errorCode.substringToIndex(1)
            firstLetter = firstLetter.uppercaseString
            errorCode = errorCode.stringByReplacingCharactersInRange(NSMakeRange(0, 1), withString: firstLetter)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多