picture

Objective-C: NSDateFormatter-usage

April 30th, 2014

The usage of NSDateFormatter is well explained at:

http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/

To build up your custom NSDateFormatter strings, use following syntax:

a: AM/PM
A: 0~86399999 (Millisecond of Day)

c/cc: 1~7 (Day of Week)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

d: 1~31 (0 padded Day of Month)
D: 1~366 (0 padded Day of Year)

e: 1~7 (0 padded Day of Week)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat
EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

F: 1~5 (0 padded Week of Month, first day of week = Monday)

g: Julian Day Number (number of days since 4713 BC January 1)
G~GGG: BC/AD (Era Designator Abbreviated)
GGGG: Before Christ/Anno Domini

h: 1~12 (0 padded Hour (12hr))
H: 0~23 (0 padded Hour (24hr))

k: 1~24 (0 padded Hour (24hr)
K: 0~11 (0 padded Hour (12hr))

L/LL: 1~12 (0 padded Month)
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
LLLL: January/February/March/April/May/June/July/August/ September/October/November/December

m: 0~59 (0 padded Minute)
M/MM: 1~12 (0 padded Month)
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM: January/February/March/April/May/June/July/August/ September/October/November/December

q/qq: 1~4 (0 padded Quarter)
qqq: Q1/Q2/Q3/Q4
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter
Q/QQ: 1~4 (0 padded Quarter)
QQQ: Q1/Q2/Q3/Q4
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter

s: 0~59 (0 padded Second)
S: (rounded Sub-Second)

u: (0 padded Year)

v~vvv: (General GMT Timezone Abbreviation)
vvvv: (General GMT Timezone Name)

w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)

W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)

y/yyyy: (Full Year)
yy/yyy: (2 Digits Year)
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)

z~zzz: (Specific GMT Timezone Abbreviation)
zzzz: (Specific GMT Timezone Name)
Z: +0000 (RFC 822 Timezone)

Objective-C: Compare NSDate-objects

April 29th, 2014

If you have to compare tow dates or/and especially to determine whether a particular date is in the past, present or future, use the code below:

NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:particularDate];
switch (result) {
    case NSOrderedAscending: 
        NSLog(@"Date is in the future");
        break;
    case NSOrderedDescending: 
        NSLog(@"Date is in the past");
        break;
    case NSOrderedSame: 
        NSLog(@"Today/Null Date Passed"); 
        break;
    default: 
        NSLog(@"Error Comparing Dates");
        break;
}

CoreData: Transient Properties Link

April 29th, 2014

The link below point to a nice post about transient properties in CoreData:

http://davemeehan.com/technology/objective-c/core-data-transient-properties-on-nsmanagedobject

Objective-C: Difference between weak and assign

April 1st, 2014

So, what is the difference between weak and assign?

@property (nonatomic, weak)id<YourDelegate> delegate;

VS.

@property (nonatomic, assign) id<YourDelegate> delegate;

As I understand, the only difference between weak and assign is that if the object of a weak property is deallocated, the value of the weak pointer will be set to nil. Using weak properties prevents accessing memory garbage.

If you use assign, and the value object is deallocated, the value of the assign-property points to garbage.

I haven’t come across use cases so far, when the usage of assign-properties would be of advantage. My recommendation: use weak-properties to avoid problems! ;-)

Open Source Programs for MAC and PC

March 24th, 2014

I’m subscribed to XING`s IT-news which I regularly get per email. Today I found a nice article about useful free software for MAC and PC which I’d like to repost:

http://www.moneysavingexpert.com/shopping/free-office-software

Tools: Tidy XML with TextWrangler

January 27th, 2014

The following tip is for TextWrangler users of version 4.5.6 and greater. If you want XML files look nice in TextWrangler follow these instructions:

- create a new script file, e.g. TidyXml.sh with following content


#!/bin/sh
XMLLINT_INDENT=$'\t' xmllint --format --encode utf-8 -

and save it under

/Users/[UserFolder]/Library/Application Support/TextWrangler/Text Filters

That’s all! now just open your XML file, got to menu Text->Apply Text Filter and choose your script file.

iOS: Singleton Pattern with Objective-C

December 27th, 2013

Find below the way Apple recommends to implement singleton in your code. In your .h file you should declare a static method:

+ (YourClassName*)sharedInstance; 

Afterwards go to your .m file and implement the declared static method:

+ (YourClassName*)sharedInstance 
{
	// Create a singelton using GCD (thread safe!)
	static YourClassName * sharedInstance = nil;
	static dispatch_once_t onceToken = 0;
	dispatch_once(&onceToken, ^{
		sharedInstance = [[YourClassName alloc] init];
	});
	return sharedInstance;
}

This is a thread safe implementation of the singleton pattern! I always used Java-like singleton implementation in the past, but then I found the described way.