SwiftRandom 1.0.0

SwiftRandom 1.0.0

Tests 测试
Lang 语言 Swift Swift
许可协议 MIT
Released 最后发布2016年10月
SPM 支持 SPM

Furkan Yilmaz 维护。



  • 作者:
  • thellimist

SwiftRandom

SwiftRandom 是一个用于生成随机数据的迷你辅助库,例如

  • 随机人类事物,如:姓名、性别、头衔、标签、对话
  • 随机数据类型,如:Int、CGFloat、Bool、UIColor、NSDate、NSURL、数组中的元素

使用方法

func asExtension() {
    Int.random(2, 77) // Random between 2-77
    Int.random(2...77)
    Int32.random(13, 37) // Random between 13-37
    Int32.random(13...37)
    Double.random()
    Float.random(3.2, 4.5) // Random between 3.2-4.5
    CGFloat.random()
    NSDate.random()
    NSDate.randomWithinDaysBeforeToday(7)
    UIColor.random()
    NSURL.random()

    // Array Extensions
    var someArray = ["hello", "world"]
    someArray[0..<someArray.count].randomItem() // Array slice
    someArray.randomItem()
}
func asMethods() {
    // Return random Int >= 10 and <= 20
    // Example Output: 13
    Randoms.randomInt(10,20)

    // Return random Int32 >= 10 and <= 20
    // Example Output: 13
    Randoms.randomInt32(10,20)

    // Return random Double >= 10 and <= 20
    // Example Output: 11.511219042938
    Randoms.randomDouble(10,20)

    // Return random Float >= 10 and <= 20
    // Example Output: 17.0361
    Randoms.randomFloat(10,20)

    // Return random CGFloat between 1 >= and >= 0
    // Example Output: 0.622616
    Randoms.randomCGFloat()

    // Return true 30%, false %70
    // Example Output: false
    Randoms.randomPercentageisOver(70)

    // Return true or false
    // Example Output: false
    Randoms.randomBool()

    // Return random NSDate today > and > today - 7.
    // Example Output: 2015-10-08 03:55:09 +0000
    Randoms.randomDateWithinDaysBeforeToday(7)

    // Random Date since 1970
    // Example Output: 1997-02-01 15:27:08 +0000
    Randoms.randomDate()

    // Return UIColor. Alpha channel always 1.
    // Example Output: UIDeviceRGBColorSpace 0.645737 0.126625 0.52535 1
    Randoms.randomColor()

    // Return random NSURL
    // Example Output: http://leagueoflegends.com/
    Randoms.randomNSURL()

    // ==================== Fake Generators for Fake Datasources ==================== //

    // Return random name
    // Example Output: "Megan Freeman"
    Randoms.randomFakeName()

    // Return random fake name prefixed by English honorific
    // Example Output: "Dr. Megan Freeman"
    Randoms.randomFakeNameAndEnglishHonorific()

    // Return "Male" or "Female" as String
    // Example Output: "Female"
    Randoms.randomFakeGender()

    // Return random conversation
    // Example Output: "No! I'm tired of doing what you say."
    Randoms.randomFakeConversation()

    // Return random title
    // Example Output: "B2 Pilot @ USAF"
    Randoms.randomFakeTitle()

    // Return random tag as string
    // Example Output: "question"
    Randoms.randomFakeTag()

    // Return random currency as String
    // Example Output: "EUR"
    Randoms.randomCurrency()

    // Return random (non-existing) gravatar as UIImage?
    // The image is optional in case of network issues
    Randoms.randomGravatar { (image, error) -> Void in
        // Handle the image/error
    }

    // For consistance behaviour you can create custom Gravatar
    Randoms.createGravatar(Randoms.GravatarStyle.Retro) { (image, error) -> Void in
        // Handle the image/error
    }
}

需求

  • Swift 版本 2.0

安装

手动安装

  • 下载并将 'Randoms.swift' 放入您的项目中。

改进

  • 欢迎添加您自己的随机数据函数,并发送pull请求。
可能的特性
  • 随机野生动物图片(不应在项目中包含图片,需要在需要时从网络加载)
  • 使 OSX 兼容,并在以下链接处添加: https://github.com/AndrewSB/awesome-osx
  • 随机 JSON
  • 随机 GIF(不应在项目中包含 GIF,需要在需要时从网络加载)
  • 随机视频(不应在项目中包含视频,需要在需要时从网络加载)
  • SwiftRandom 实现为一个任何类都可以遵守的协议(例如 Post.random() 会为您提供一篇随机文章)

许可协议

  • SwiftRandom 可在 MIT 许可协议下使用。请参阅 LICENSE 文件

关键词

random, swift, data, generator, faker, fake, gravatar