Skip to content

Instantly share code, notes, and snippets.

@tsbob
tsbob / gist:3213533
Created July 31, 2012 04:10
NSDictionary with json
@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:
(NSString*)urlAddress;
-(NSData*)toJSON;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:
(NSString*)urlAddress
{
@tsbob
tsbob / gist:3213239
Created July 31, 2012 03:23
nsstring 2 nsdata
NSString* aStr = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
NSData* aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];
//If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
@tsbob
tsbob / gist:3187137
Created July 27, 2012 09:43
string to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM"];
NSDate *date = [dateFormat dateFromString:@"2012-4"];
//1、字符串转换为日期
  NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
NSDate *date =[dateFormat dateFromString:@"1980-01-01 00:00:01"];
@tsbob
tsbob / gist:3181214
Created July 26, 2012 09:31
优化字符串定义的宏
#define OC(str) [NSString stringWithCString:(str) encoding:NSUTF8StringEncoding]
#ifdef DEBUG
# define LOG(fmt, ...) do { \
NSString* file = [[NSString alloc] initWithFormat:@"%s", __FILE__]; \
NSLog((@"%@(%d) " fmt), [file lastPathComponent], __LINE__, ##__VA_ARGS__); \
[file release]; \
} while(0)
# define LOG_METHOD NSLog(@"%s", __func__)
# define LOG_CMETHOD NSLog(@"%@/%@", NSStringFromClass([self class]), NSStringFromSelector(_cmd))
@tsbob
tsbob / gist:3180668
Created July 26, 2012 07:03
Singletons 实现
Here's what the GCD (and ARC) version looks like:
+ (id)sharedInstance
{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init]; // or some other init method
});
return _sharedObject;
}
@tsbob
tsbob / ExampleClass.m
Created July 26, 2012 07:03 — forked from lukeredpath/ExampleClass.m
Macro for creating your "shared instance" using GCD
@implementation MySharedThing
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
@end