This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Objective-C | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if ([segue.identifier isEqualToString:@"Item Detail"]) { | |
DetailViewController *d = (DetailViewController *)segue.destinationViewController; | |
d.title = segue.identifier; | |
d.itemForDetail = item; | |
} else if ([segue.identifier isEqualToString:@"Related Items"]) { | |
UINavigationController *nc = segue.destinationViewController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func formatNumberAsSpelledOutString(number: Any) -> String { | |
var numberString: String! | |
let spellOutFormatter = NSNumberFormatter() | |
spellOutFormatter.numberStyle = .SpellOutStyle | |
if number is Int { | |
let num = number as Int | |
numberString = spellOutFormatter.stringFromNumber(num) | |
} else if number is Double { | |
spellOutFormatter.minimumFractionDigits = 1 | |
let num = number as Double |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Introspector { | |
var values: Any! | |
subscript(theValues: Int...) -> (sum: Int, average: Double) { | |
values = theValues | |
var sum = 0 | |
for integer in theValues { | |
sum += integer | |
} | |
let average = Double(sum) / Double(theValues.count) | |
return (sum: sum, average: average) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol HasMiddleValue { | |
typealias ItemType | |
func middle() -> [ItemType] | |
} | |
extension Array: HasMiddleValue { | |
typealias ItemType = Generator.Element |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testCopyingLists() { | |
if let listCopy = list.copy() as? List { | |
XCTAssertEqual(list, listCopy, "listCopy should be equal to list") | |
} else { | |
XCTFail("listCopy should not be nil") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Address { | |
static let zipPlus4RegEx = NSRegularExpression(pattern: "\\-\\d{4}", options: nil, error: nil)! | |
static func formattedAddressForPlacemark(placemark: CLPlacemark, includeName: Bool = true, withLineBreaks: Bool = true, showCountryIfUSA: Bool = false) -> String { | |
var addressArray = [String]() | |
var index = 0 | |
if placemark.name != nil && includeName { addressArray.append(placemark.name) } | |
if placemark.subLocality != nil { addressArray.append(placemark.subLocality) } // Neighborhood |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
/** | |
Checks if string is a palindrome, ignoring spaces and capitalization. [Source on GitHub](http://bit.ly/SwiftStringPalindromeExtension) | |
- note: | |
[What's a palindrome?](https://en.wikipedia.org/wiki/Palindrome) | |
- returns: Bool | |
*/ | |
public func isPalindrome() -> Bool { | |
var s = String(self.characters.filter { $0 != Character(" ") }).lowercased() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/** | |
Converts any numeric literal (e.g., 1) or string containing a numeric literal (e.g., "1"), into a spelled-out number string (e.g., "one"). [Source on GitHub](http://bit.ly/SwiftSpellOutNumber) | |
- parameter number: a numeric literal, or string containing a numeric literal | |
- returns: String? | |
*/ | |
public func spellOut<N>(number: N) -> String? { | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .spellOut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Int { | |
/** | |
`true` if self is a prime number, i.e., can only be divided evenly by 1 and itself. | |
- author: Scott Gardner | |
- seealso: | |
* [Source on GitHub](http://bit.ly/SwiftIntIsPrimeExtension) | |
*/ | |
public var isPrime: Bool { | |
guard self > 1 else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ==(x: HashableType, y: HashableType) -> Bool { | |
return x.isEqual(y.value) | |
} | |
struct HashableType: Hashable { | |
let value: Any | |
var hashValue: Int { | |
return getHashValue() | |
} |
OlderNewer