This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation MySharedThing | |
+ (id)sharedInstance | |
{ | |
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{ | |
return [[self alloc] init]; | |
}); | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSDictionary(JSONCategories) | |
+(NSDictionary*)dictionaryWithContentsOfJSONURLString: | |
(NSString*)urlAddress; | |
-(NSData*)toJSON; | |
@end | |
@implementation NSDictionary(JSONCategories) | |
+(NSDictionary*)dictionaryWithContentsOfJSONURLString: | |
(NSString*)urlAddress | |
{ |