在Swift中生成一个随机单词 [英] Generate a Random Word in Swift
问题描述
我正在尝试探索Swift编程语言。我正在搜索Swift API,我找到了 UIReferenceLibraryViewController
类。如果单词是真的( .dictionaryHasDefinitionForTerm
),我找到了返回bool值的方法,并且我还查找了一个可以返回随机单词的方法。
I am trying to explore the Swift programming language. I was searching through the Swift API and I found the UIReferenceLibraryViewController
class. I found the method that returns a bool value if a word is real or not (.dictionaryHasDefinitionForTerm
) and I also looked for a method that can return a random word.
可悲的是,这种方法似乎并不存在。我意识到我可以探索第三方API,但如果可能的话,我更愿意远离它们。
Sadly, this method does not seem to exist. I realize that I can explore 3rd party APIs, however I prefer to stay away from them if possible.
我想也许我可以通过所有字母的随机排列,然后检查它们是否形成一个真实的单词,但这似乎......好吧......愚蠢。
I thought that maybe I could go through random permutations of all letters and then check if they form a real word, but this seems... well... stupid.
有人知道生成随机单词的方法吗?
Does anybody know of a way to generate a random word?
我也不想手动制作数千个单词的长列表,因为我担心内存错误。我想尝试学习一些语法和新方法,而不是如何导航列表。
I also do not want to manually make a long list of thousands of words because I fear a memory error. I want to try to also learn some syntax and new methods, not how to navigate lists.
推荐答案
我的 / usr / share / dict / words
file是指向 / usr / share / dict / words / web2
的符号链接,Webster's Second International Dictionary从1934年开始。文件只有2.4mb,因此你不应该看到太多的性能损失将整个内容加载到内存中。
My /usr/share/dict/words
file is a symbolic link to /usr/share/dict/words/web2
, Webster's Second International Dictionary from 1934. The file is only 2.4mb, so you shouldn't see too much of a performance hit loading the entire contents into memory.
这是一个小的Swift 3.0代码片段我写了从字典文件中加载一个随机单词。请记住在运行之前将文件复制到应用程序包中。
Here's a small Swift 3.0 snippet I wrote to load a random word from the dictionary file. Remember to copy the file to your Application's bundle before running.
if let wordsFilePath = Bundle.main.path(forResource: "web2", ofType: nil) {
do {
let wordsString = try String(contentsOfFile: wordsFilePath)
let wordLines = wordsString.components(separatedBy: .newlines)
let randomLine = wordLines[numericCast(arc4random_uniform(numericCast(wordLines.count)))]
print(randomLine)
} catch { // contentsOfFile throws an error
print("Error: \(error)")
}
}
Swift 2.2:
Swift 2.2:
if let wordsFilePath = NSBundle.mainBundle().pathForResource("web2", ofType: nil) {
do {
let wordsString = try String(contentsOfFile: wordsFilePath)
let wordLines = wordsString.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
let randomLine = wordLines[Int(arc4random_uniform(UInt32(wordLines.count)))]
print(randomLine)
} catch { // contentsOfFile throws an error
print("Error: \(error)")
}
}
Swift 1.2代码段:
Swift 1.2 snippet:
if let wordsFilePath = NSBundle.mainBundle().pathForResource("web2", ofType: nil) {
var error: NSError?
if let wordsString = String(contentsOfFile: wordsFilePath, encoding: NSUTF8StringEncoding, error: &error) {
if error != nil {
// String(contentsOfFile: ...) failed
println("Error: \(error)")
} else {
let wordLines = wordsString.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
let randomLine = wordLines[Int(arc4random_uniform(UInt32(wordLines.count)))]
print(randomLine)
}
}
}
这篇关于在Swift中生成一个随机单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!