Skip to content

Commit e2b706a

Browse files
committedAug 30, 2016
Add function to return random array element
Returns a random element from the array. Can be used to create a playful message that cycles randomly through a set of emoji icons, for example.
1 parent 7ca2ba5 commit e2b706a

File tree

4 files changed

+157
-127
lines changed

4 files changed

+157
-127
lines changed
 

‎Demo/Demo/ExploreViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class ExploreViewController: UITableViewController, UITextFieldDelegate {
4949
case 3:
5050
view.configureTheme(.Error, iconStyle: iconStyle)
5151
default:
52-
view.configureTheme(backgroundColor: UIColor.purpleColor(), foregroundColor: UIColor.whiteColor(), iconImage: nil, iconText: "🐸")
52+
let iconText = ["🐸", "🐷", "🐬", "🐠", "🐍", "🐹", "🐼"].sm_random()
53+
view.configureTheme(backgroundColor: UIColor.purpleColor(), foregroundColor: UIColor.whiteColor(), iconImage: nil, iconText: iconText)
5354
view.button?.setImage(Icon.ErrorSubtle.image, forState: .Normal)
5455
view.button?.setTitle(nil, forState: .Normal)
5556
view.button?.backgroundColor = UIColor.clearColor()

‎Demo/Pods/Pods.xcodeproj/project.pbxproj

+130-126
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎SwiftMessages.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
86BBA9061D5E040C00FE8F16 /* Identifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 864495551D4F7C390056EB2A /* Identifiable.swift */; };
2727
86BBA9071D5E040C00FE8F16 /* MarginAdjustable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */; };
2828
86BBA9081D5E040C00FE8F16 /* Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF82A1D580DD70031EE32 /* Error.swift */; };
29+
86DBE0041D75BE800071E51D /* Array+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86DBE0031D75BE800071E51D /* Array+Utils.swift */; };
2930
E6E49F911D70A344006CB883 /* MessageView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 862C0CDA1D5A397F00D06168 /* MessageView.xib */; };
3031
E6E49F921D70A349006CB883 /* StatusLine.xib in Resources */ = {isa = PBXBuildFile; fileRef = 862C0CDB1D5A397F00D06168 /* StatusLine.xib */; };
3132
E6E49F931D70A34C006CB883 /* MessageViewIOS8.xib in Resources */ = {isa = PBXBuildFile; fileRef = 862C0CE21D5A3A0D00D06168 /* MessageViewIOS8.xib */; };
@@ -70,6 +71,7 @@
7071
86B48AFA1D5A41C900063E2B /* SwiftMessagesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftMessagesTests.swift; sourceTree = "<group>"; };
7172
86B48AFC1D5A41C900063E2B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7273
86BBA8F81D5E01FC00FE8F16 /* CardView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = CardView.xib; path = Resources/CardView.xib; sourceTree = "<group>"; };
74+
86DBE0031D75BE800071E51D /* Array+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+Utils.swift"; sourceTree = "<group>"; };
7375
/* End PBXFileReference section */
7476

7577
/* Begin PBXFrameworksBuildPhase section */
@@ -113,6 +115,7 @@
113115
864495551D4F7C390056EB2A /* Identifiable.swift */,
114116
86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */,
115117
86AAF82A1D580DD70031EE32 /* Error.swift */,
118+
86DBE0031D75BE800071E51D /* Array+Utils.swift */,
116119
);
117120
name = Base;
118121
sourceTree = "<group>";
@@ -309,6 +312,7 @@
309312
86589D471D64B6E40041676C /* BaseView.swift in Sources */,
310313
86BBA9071D5E040C00FE8F16 /* MarginAdjustable.swift in Sources */,
311314
867BED211D622793005212E3 /* BackgroundViewable.swift in Sources */,
315+
86DBE0041D75BE800071E51D /* Array+Utils.swift in Sources */,
312316
);
313317
runOnlyForDeploymentPostprocessing = 0;
314318
};

‎SwiftMessages/Array+Utils.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Array+Utils.swift
3+
// SwiftMessages
4+
//
5+
// Created by Tim Moose on 8/30/16.
6+
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
7+
//
8+
9+
import Darwin
10+
11+
public extension Array {
12+
13+
/**
14+
Returns a random element from the array. Can be used to create a playful
15+
message that cycles randomly through a set of emoji icons, for example.
16+
*/
17+
public func sm_random() -> Generator.Element? {
18+
guard count > 0 else { return nil }
19+
return self[Int(arc4random_uniform(UInt32(count)))]
20+
}
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.