如果我有例如:
var sillyString = "This is a string!" let yellyString = sillyString.uppercaseString let silentString = sillyString.lowercaseString
我想采取sillyString并将其变为大写或小写。我现在怎么办呢?
提前致谢!
字符串无缝地桥接到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!