Swift .uppercaseString或.lowercaseString属性替换

前端之家收集整理的这篇文章主要介绍了Swift .uppercaseString或.lowercaseString属性替换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
因为Swift中的字符串不再具有.uppercaseString或.lowercaseString属性,我将如何执行该函数

如果我有例如:

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

我想采取sillyString并将其变为大写或小写。我现在怎么办呢?

提前致谢!

Xcode 6.0 / Swift 1.0

字符串无缝地桥接到NSString,所以它确实有uppercaseString和lowercaseString属性,只要你导入Foundation(或几乎任何框架,因为他们通常会导入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中,uppercaseString和lowercaseString在Swift的String类中,所以你不需要使用NSString中定义的那些。但是,它被实现为对Foundation框架中的String类的扩展,所以解决方案仍然是相同的:import Foundation

在操场上:

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!
原文链接:https://www.f2er.com/swift/321196.html

猜你在找的Swift相关文章