Posts

Showing posts from March, 2015

Gradient UINavigationBar in Swift

Image
Here's how to make a gradient NavBar in Swift, taken mainly from this <a href="http://stackoverflow.com/questions/494982/uinavigationbar-tintcolor-with-gradient">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.

http://json2csharp.com/

How to shuffle an array in Swift

4-2-2015 This was imported over from my roon.io 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:
date:1420420409680
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…