SwiftRandom 是一个用于生成随机数据的微型助手套件,例如:
请注意,最初的客户端 Swift 2 仓库可以在这里找到:https://github.com/thellimist/SwiftRandom
此 fork 旨在作为服务器端实用程序。
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()
Date.random()
Date.randomWithinDaysBeforeToday(7)
URL.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 random URL
// Example Output: http://leagueoflegends.com/
Randoms.randomURL()
// ==================== 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()
}
###要求
Package.swift
文件中// Swift 3
.Package(url: "https://github.com/iamjono/SwiftRandom.git", majorVersion: 1)
// Swift 4
.package(url: "https://github.com/iamjono/SwiftRandom.git", from: "1.0.0")
然后,重新生成您的 Xcode 项目
swift package generate-xcodeproj
###许可证
##关键字 随机, swift, 数据, 生成器, 伪造, 假数据