February 2012
1 post
1 tag
Supressing Xcode static analyzer warnings
#ifndef __clang_analyzer__ // Code not to be analyzed #endif
Feb 15th
October 2011
1 post
1 tag
Perform a Block after Delay
- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay { int64_t delta = (int64_t)(1.0e9 * delay); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta), dispatch_get_main_queue(), block); } Then, we use: [self performBlock: ^{ //Lines of code in block to execute } afterDelay: 0.5f];
Oct 23rd
1 note
August 2011
1 post
1 tag
Setting to nil
NSObject *obj; if (obj != nil) { //code will enter here and we'll get a bad access error to obj! } vs. NSObject *obj = nil; if (obj != nil) { //obj is nil so we're safe if we do something with obj }
Aug 18th
February 2011
1 post
2 tags
Hex String to Color (ARGB)
public Color HexStringToColor(string hex) { hex = hex.Replace("#", "").ToLowerInvariant(); if (hex.Length == 6) { hex = "ff" + hex; } else if (hex.Length != 8) { throw new Exception(hex + " is not a valid 6 or 8-place hexadecimal color code."); } int a, r, g, b; a = Convert.ToInt32(hex.Substring(0, 2), 16); r = Convert.ToInt32(hex.Substring(2, 2), 16); g =...
Feb 24th
July 2010
1 post
1 tag
Swapping variable values using multiple assignment...
Lua allows multiple assignment, where a list of values is assigned to a list of variables in one step. Both lists have their elements separated by commas. For instance, in the assignment a, b = 10, 2*x the variable a gets the value 10 and b gets 2*x. In a multiple assignment, Lua first evaluates all values and only then executes the assignments. Therefore, we can use a multiple assignment...
Jul 25th
April 2010
1 post
2 tags
NSDate to DateTime and Back
Reference Date is January 1, 2001 0:00:00 UTC (GMT) It’s important to note the Local Time Zone vs. UTC/GMT NSDate to DateTime public static DateTime NSDateToDateTime(NSDate date) { DateTime reference = TimeZone.CurrentTimeZone.ToLocalTime( new DateTime(2001, 1, 1, 0, 0, 0) ); return reference.AddSeconds(date.SecondsSinceReferenceDate); } DateTime to NSDate public...
Apr 6th
2 notes
February 2010
3 posts
3 tags
Strip HTML Tags
using System.Text.RegularExpressions; ... public static string StripHTML(string htmlText) { return Regex.Replace(htmlText, "<(.|\n)*?>", ""); }
Feb 18th
“Always code as if the guy who ends up maintaining your code will be a violent...”
– Martin Golding (?)
Feb 16th
2 tags
Unicode Date Format Patterns →
Feb 4th
January 2010
2 posts
3 tags
Integer to Ordinal
public string AddOrdinal(int num) { switch(num % 100) { case 11: case 12: case 13: return num.ToString() + "th"; } switch(num % 10) { case 1: return num.ToString() + "st"; case 2: return num.ToString() + "nd"; case 3: return num.ToString() + "rd"; default: return...
Jan 21st
1 tag
age from birthday
Rather than dividing figures by a fixed number days (which ignores special cases like leap years), I find this method quite reliable. //Given DateTime birthday TimeSpan span = DateTime.Now - birthday; DateTime time = DateTime.MinValue + span; int age = time.Year - 1;
Jan 20th
2 notes
August 2009
2 posts
1 tag
“Any fool can write code that a computer can understand. Good programmers write...”
– Martin Fowler
Aug 24th
1 tag
System.IO.Path.Combine Behavior
When using Path.Combine, you should be aware of the behavior that if the second parameter has a leading slash, it will ignore the first parameter. Given the following: string root = "c:\"; string path = "test.txt"; string lead = "\test.txt"; We get the following results: using System.IO; … Path.Combine(root, path); // ‘c:\test.txt’ Path.combine(root, lead); ...
Aug 23rd
June 2009
2 posts
1 tag
3 A's Pattern For Writing Test Methods
Arrange, Act, Assert. Specifically, use separate code paragraphs (groups of lines of code separated by a blank line) for each of the As. Arrange is variable declaration and initialization. Act is invoking the code under test. Assert is using the Assert.* methods to verify that expectations were met. Following this pattern consistently makes it easy to revisit test code. [source: Principles...
Jun 21st
1 tag
HTTP GET for ASP.NET Webservices
It’s disabled by default and to enable it, add this to the web.config <configuration> <system.web> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> </system.web> </configuration> Alternatively, you can enable these protocols...
Jun 16th
May 2009
3 posts
1 tag
Safe Casting
Use the as keyword. MyClass castedObject = obj as MyClass; This won’t throw a class cast exception. Instead, castedObject will return null if obj isn’t a MyClass.
May 4th
1 tag
Chain ?? Operators
You can use multiple ?? operators to chain multiple null comparisons: string text = value1 ?? value2 ?? String.Empty;
May 4th
1 tag
Normalizing & Comparing Strings
When normalizing strings, it is highly recommended that you use ToUpperInvariant instead of ToLowerInvariant because Microsoft has optimized the code for performing uppercase comparisons. [source: CLR via C# ]
May 4th