Showing posts from March, 2015

Gradient UINavigationBar in Swift

Here's how to make a gradient NavBar in Swift, taken mainly from this <a href="">SO post</a>: First create an extension for CAGradientLayer Here's how to make a gradient NavBar in Swift:
First create an extension for CAGradientLayer (need to add Quartz library)
extension CAGradientLayer { class func gradientLayerForBounds(bounds: CGRect) -> CAGradientLayer { var layer = CAGradientLayer() layer.frame = bounds layer.colors = [UIColor.redColor().CGColor, UIColor.blueColor().CGColor] return layer } } Use whatever colors suit your needs.
Then in your overriden NavController class (so many ways to do this, I chose to set it up in the NavController subclass used everywhere in my app),
private func imageLayerForGradientBackground() -> UIImage { var updatedFrame = self.navigationBar.bounds // take into account the status bar …

C# Model Object builder

A nice online tool to build C# model objects based off JSON text you feed it.

How to shuffle an array in Swift

4-2-2015 This was imported over from my blog that's now been closed down.

Had the challenge and time limit put to me the other day so here’s my working solution:
func shuffleArray(array: [Int]) -> [Int] { var tempArray = array for index in 0...array.count - 1 { let randomNumber = arc4random_uniform(UInt32(myArray.count - 1)) let randomIndex = Int(randomNumber) tempArray[randomIndex] = array[index] } return tempArray } shuffleArray(myArray) shuffleArray(myArray) shuffleArray(myArray)

Parsing a date out of JSON in Swift

For most of the APIs you'll deal with you'll get the date back in a For most of the APIs you'll deal with you'll get the date back in a format like this:
In Objective-C it was pretty easy assume the interval is the value above,
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval/1000.0]; NSDateFormatter* df_local = [[NSDateFormatter alloc] init]; [df_local setTimeZone:[NSTimeZone timeZoneWithName:@"CST"]]; [df_local setDateFormat:@"MM-dd"]; NSString* ts_local_string = [df_local stringFromDate:date]; This would give you a formatted date string in the timezone you request.
In Swift it wasn't as straight forward.
Originally I attempted this:
var meetups = [Meetup]() for meetupDictionary in (meetupsJsonArray as Array<Dictionary<String, AnyObject>>) { let eventName = meetupDictionary[self.nameKey]! as String let eventId = meetupDictionary[self.idKey]! as String let timeValue = meetupDi…