Generate a Random Word in Swift

后端 未结 2 1632
终归单人心
终归单人心 2021-01-06 03:30

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

相关标签:
2条回答
  • 2021-01-06 04:12

    I suggest you to check this project. A guy have already done the following for you!

    LoremSwiftum

    LoremSwiftum is a lightweight lorem ipsum generator for iOS written in Swift. It supports generating texts in different formats (words, sentences, paragraphs), miscellaneous data (names, URLs, dates etc.) and placeholder images for iOS (UIImage). This is a reimplementation of the project LoremIpsum written in Objective-C.

    https://github.com/lukaskubanek/LoremSwiftum

    This project has only single swift file.( ~300 lines) Therefore, I think reading the file will help you.

    https://github.com/lukaskubanek/LoremSwiftum/blob/master/Sources/LoremSwiftum.swift

    0 讨论(0)
  • 2021-01-06 04:23

    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.

    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:

    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 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)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题