Posts from the “iOS” Category

Weakify Self

Note: weakifying self in a block has a few reasons.

If the block is retained by self, weakifying self helps avoiding retain cycle.

Otherwise, if the block is not retained by self, WE STILL WANNA WEAKIFY SELF because we don’t wanna anything unknown to retain self especially when self is complicated (i.e. view controller). Retaining self by in a block may ultimately delay the release of the instance of type to which self belongs.

Conclusion is, always weakify self in blocks. But remembering the reasons is important to avoid not weakifying when some reasons are forgotten.

Convert NSData to NSString

NSData -> NSString:

Ignore stringWithUTF8String and use initWithData:encoding:NSUTFStringEncoding because stringWithUTF8String expects a NUL terminated buffer whereas NSData doesn’t guarantee that. Invalid NSData could cause a nil result using stringWithUTF8String.

NSDate Formatting Convenience

Go to the “NSDateFormatter” to preview the result of your format:

http://nsdateformatter.com

And refer to the following table to construct the format:

iPhone NSDateFormatter date formatting table