قالب وردپرس درنا توس
Home / IOS Development / # 285: Generate random data with RandomKit 🎲 – Little Bites of Cocoa

# 285: Generate random data with RandomKit 🎲 – Little Bites of Cocoa



Subjects

Themes

If we need trial values ​​while prototyping the app's interface, or any multipliers for the game logic random data can be incredibly useful when programming. Today we are going to check out a great library from Nikolai Vazquez called RandomKit which makes generating random data both simple and intuitive. Let us begin.

RandomKit is built on a set of Swift protocols. We are given Random RandomWithinRange RandomToValue and many more. RandomKit then extends a pile of types to comply with these protocols.

(This is good because it means we can easily add RandomKit style functionality to our own types if we ever need it. Tidy.)

Let's try out the basics. Some of the most common things to generate randomly are numbers and boolean boxes. RandomKit has got us well covered here, which supports all the big numbers Foundation types (and more):

  Double .   random   ()    // 0.6472946529383645 
 ] [19659000]]   random   [  within :    1  ... [19659022] ]     // 7 
  NSNumber       [] :    -   5  ... [19659022] 5 )     // -3 [19659013] CGFloat .   Random   (  0  ...   1 )     // 0.27969591675319 
  Bool .   random   ()    // false 

Neat. Next Up, Strings:

  String .   Random   (  ofLength :    5 )    // "$ - = 5t" 
 ]     Random   ( )    // "#" 

We can also easily take a random item from any Swift Sequence or ] :

["19659074] "" "" " 19659011] " Luke " " Chewie "] random // "Luke"
NSDictionary dictionary : [ "first name" : "He" ] "lastname" : "Solo" ]) . Random // ("LastName", "Solo")

But wait, It's more! RandomKit is incredibly comprehensive. Let's try some more advanced functionality like Dates :

  Date .   Random   (  within : [19659064] Date .  distantPast  ...   Date   ())     // "Feb 7, 472, 5:40" 

URLs:

  URL .   Random   ()    // https://stackoverflow.com 

... or even UIColors and [1Lastlywecanevengetrandomvalues​​for CoreGraphics types:

  CGPoint  19659011].   Random     :    0  ...   50     0  ...   50  ]    //     [  within :    0  ...   50       ] 0 [19659011] ... [19659011] )    // {w 29,475 h 12,863} 
  CGRect .   random   ()    // {x 3,872 y 46,15 w 8,852 h 20,201} 

These examples were just one (sorry) random sampling of RandomKit & # 39; s functionality. It has a lot more to offer.

Learn more about RandomKit at git.io/randomkit


Source link