Posts

Showing posts from 2015

Prefer Swift's for-in loop over traditional C for loop

I'm sure most of us started our first programming adventures in C. How many timesI'm sure most of us started our first programming adventures in C.How many times have you writtenfor (int i = 0; i < blah; i++) or something similar in countless methods, functions, etc.?In C, C++, Javascript, Java, it's ubiquitous.Then I came upon the most glorious programming language ever created, Rubyfor index in 0..5 puts "Value of local variable is #{i}" end This is obviously equivalent to:for (int index = 0; i <= 5; i++) The ease of reading should be obvious in Ruby's typical verbosity. Not that any programmer worth his/her salt can't immediately decipher a traditional for loop.The idea though is to flow code in a way we read natural language.So it was no suprise that Swift adopted this for-in loop pattern, and in all cases you should prefer to use this format over the C for loop.Here's Swift's for-infor index in 0...5 { print("current in…

Using Xamarin Android Player with Android Studio

Image
Android Studio and it's accompanying emulator have actually improved considerably. I'd even say the emulatorAndroid Studio and its accompanying emulator have actually improved considerably. I'd even say the emulator that ships now is perfectly sufficient to use, although there's obviously the better alternative(s) like GenyMotion.Having said that, the Xamarin Android Player is probably the best out there for a free fast booting emulator.First install the XAP, no need to install Xamarin Studio.Launch Android Studio, have your project ready to launch on an emulator.Here's thing, the running XAP (after you create a device and press play) will not show up in Android Studios list of devices in the AVD. Don't fret, as it won't matter.When you finally push the run button you will get a screen like this with an option to now launch on the XAP:Now you can use the XAP for your project :)

Simple Xamarin iOS Keychain code

I had searched and found a few decent examples of KeyChain (iOS) usage in XamarinI had searched and found a few decent examples of KeyChain (iOS) usage in Xamarin but not exactly the code I was happy with.So this is more of a mashup of pieces of the good code I found formed into one utility class.The key essentials are find an existing key, create a new key, delete a previous key. The examples I saw tried to do all of this in one method, it's better to break things up, have one method per responsilibity.using Security; using Foundation; public class KeyChain { public string ValueForKey(string key) { var record = ExistingRecordForKey (key); SecStatusCode resultCode; var match = SecKeyChain.QueryAsRecord(record, out resultCode); if (resultCode == SecStatusCode.Success) return NSString.FromData (match.ValueData, NSStringEncoding.UTF8); else return String.Empty; } public void SetValueForKey(string val…

Xamarin Studio Key bindings for the Xcode user

I was tired of switching machines using Xamarin Studio and having to reset all my keybindings. Because I'm so used to Xcode keybindings, I did my best to make the same similar key bindings for XS, so here you can grab them, make them your own:

https://github.com/markawil/XamarinStudio-KeyBindings/

Simple C# Dependency Resolver, or the beginning of your own IoC container

It's kind of pointless to write your own IoC container in C# seeing that there'sIt's kind of pointless to write your own IoC container in C# seeing that there's probably a 100 out there at this point. With that said, there are occasions like the project I'm in now where you plan on some point pulling in a popular controller, but you need something basic to register and resolve instances of interfaces throughout the existing code base in the mean time.The idea is this:You have viewModels or controllers that need services, clients, other controllers, whatever.You could just not care about clean code and do this:public class MainViewModel() { private readonly IService _service; private readonly IDataSource _source; public MainViewModel() { _service = new Service(); _source = new Source(service); } void go() { _service.CallAPI(); } } No big deal. But you start doing this in multiple controllers, viewModels, and then…

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…