Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Random Alphanumeric String generation #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions SwiftRandom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
PRODUCT_BUNDLE_IDENTIFIER = thellimist.SwiftRandom.SwiftRandomTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -253,7 +253,7 @@
PRODUCT_BUNDLE_IDENTIFIER = thellimist.SwiftRandom.SwiftRandomTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down Expand Up @@ -299,6 +299,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -339,6 +340,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -356,12 +358,13 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = SwiftRandom/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = thellimist.SwiftRandom;
PRODUCT_NAME = SwiftRandom;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -375,12 +378,13 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = SwiftRandom/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = thellimist.SwiftRandom;
PRODUCT_NAME = SwiftRandom;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -394,6 +398,7 @@
53DC009B1DA3313F00C3C823 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
B537B0C51BCA9E3E005725CE /* Build configuration list for PBXProject "SwiftRandom" */ = {
isa = XCConfigurationList;
Expand Down
18 changes: 18 additions & 0 deletions SwiftRandom/Randoms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ public extension URL {
}
}

public extension String {
public static func random(_ length: Int = 5) -> String {
let charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var char = charSet.characters.map { String($0) }
var randomString = ""

for _ in (1...length) {
randomString = randomString + char[Int(arc4random()) % char.count]
}

return randomString
}
}


public struct Randoms {

Expand Down Expand Up @@ -211,6 +225,10 @@ public struct Randoms {
public static func randomNSURL() -> URL {
return URL.random()
}

public func randomString(_ length: Int = 5) -> String {
return String.random(length)
}

//==========================================================================================================
// MARK: - Fake random data generators
Expand Down