gpt4 book ai didi

Swift .uppercaseString 或 .lowercaseString 属性替换

转载 作者:IT王子 更新时间:2023-10-29 04:58:54 25 4
gpt4 key购买 nike

由于 Swift 中的字符串不再具有可用的 .uppercaseString 或 .lowercaseString 属性,我将如何执行该功能?

如果我有例如:

var sillyString = "This is a string!"
let yellyString = sillyString.uppercaseString
let silentString = sillyString.lowercaseString

我想将 sillyString 转换为大写或小写。我现在该怎么做?

提前致谢!

最佳答案

Xcode 6.0/ swift 1.0

String 无缝桥接到 NSString,因此它确实uppercaseStringlowercaseString 属性只要你import Foundation(或者几乎任何框架,因为它们通常会在内部import Foundation。来自Strings and Characters section Swift 编程指南:

Swift’s String type is bridged seamlessly to Foundation’s NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch, the entire NSString API is available to call on any String value you create, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.


Xcode 6.1/ swift 1.1

正如@newacct 指出的那样,在 Xcode 6.1/Swift 1.1 中,uppercaseStringlowercaseString 在 Swift 的 String 类,因此您不需要使用 NSString 中定义的类。但是,它是作为 Foundation 框架中 String 类的扩展实现的,因此解决方案仍然相同:import Foundation

在 Playground 上:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercaseString // --> THIS IS A STRING!
let silentString = sillyString.lowercaseString // --> this is a string!

swift 3.0

在 Playground 上:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercased() // --> THIS IS A STRING!
let silentString = sillyString.lowercased() // --> this is a string!

关于Swift .uppercaseString 或 .lowercaseString 属性替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com