replacementOccurrences错误

R W*_*rld 1 string ios swift swift2

从udacity课程我尝试下面的代码

 let password = "Meet me in St. Louis"
 let newPassword = password.replacingOccurrences(of: "e", with: "3")
Run Code Online (Sandbox Code Playgroud)

这是给予

Playground execution failed: Introduction.xcplaygroundpage:31:19: error: value of type 'String' has no member 'replacingOccurrences'
let newPassword = password.replacingOccurrences(of: "e", with: "3")
              ^~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我必须使用

let password = "Meet me in St. Louis"
let newPassword = password.stringByReplacingOccurrencesOfString("me", withString: "ss")
Run Code Online (Sandbox Code Playgroud)

任何线索为什么会出现这个错误?我使用的是swift 2.2版

Dmi*_*lev 7

如果您从命令行运行此代码示例(通过运行Swift REPL),请import Foundation在.swift文件的开头添加.


nan*_*eck 6

确保你有 Swift 版本 3+

另外,请确保您:

 import Foundation
Run Code Online (Sandbox Code Playgroud)