Last active
August 29, 2015 14:01
-
-
Save wangzz/de7c9d52bb4d7b29107b to your computer and use it in GitHub Desktop.
记录有用的代码片段
This file contains 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
记录有用的代码片段 |
This file contains 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
it is a test file. |
自定义系统NSLog输出:
#define NSLog(format, ...) do {\
fprintf(stderr, "<%s : %d> %s\n",\
[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],\
__LINE__, __func__);\
(NSLog)((format), ##__VA_ARGS__);\
fprintf(stderr, "-------\n");\
} while (0)
判断当前对象有没有被释放掉:
Class currentClass = object_getClass(self.delegate);
if (currentClass == _originalClass) {
// 如果delegate没有被释放
}
判断手机硬件型号:
#import <sys/utsname.h>
- (NSString*) deviceName
{
struct utsname systemInfo;
uname(&systemInfo);
NSString* code = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
static NSDictionary* deviceNamesByCode = nil;
if (!deviceNamesByCode) {
deviceNamesByCode = @{@"i386" :@"Simulator",
@"x86_64" :@"Simulator",
@"iPod1,1" :@"iPod Touch", // (Original)
@"iPod2,1" :@"iPod Touch 2G", // (Second Generation)
@"iPod3,1" :@"iPod Touch 3G", // (Third Generation)
@"iPod4,1" :@"iPod Touch 4G", // (Fourth Generation)
@"iPod5,1" :@"iPod Touch 5G",
@"iPhone1,1" :@"iPhone", // (Original)
@"iPhone1,2" :@"iPhone 3G", // (3G)
@"iPhone2,1" :@"iPhone 3GS", // (3GS)
@"iPhone3,1" :@"iPhone 4", //
@"iPhone3,2" :@"iPhone 4",
@"iPhone3,3" :@"iPhone 4",
@"iPhone4,1" :@"iPhone 4S", //
@"iPhone5,1" :@"iPhone 5", // (model A1428, AT&T/Canada)
@"iPhone5,2" :@"iPhone 5", // (model A1429, everything else)
@"iPhone5,3" :@"iPhone 5c", // (model A1456, A1532 | GSM)
@"iPhone5,4" :@"iPhone 5c", // (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" :@"iPhone 5s", // (model A1433, A1533 | GSM)
@"iPhone6,2" :@"iPhone 5s", // (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPhone7,1" :@"iPhone 6 Plus",
@"iPhone7,2" :@"iPhone 6",
@"iPad1,1" :@"iPad", // (1th Generation)
@"iPad2,1" :@"iPad 2",
@"iPad2,2" :@"iPad 2",
@"iPad2,3" :@"iPad 2",
@"iPad2,4" :@"iPad 2",
@"iPad3,1" :@"iPad 3",
@"iPad3,2" :@"iPad 3",
@"iPad3,3" :@"iPad 3",
@"iPad3,4" :@"iPad 4",
@"iPad3,5" :@"iPad 4",
@"iPad3,6" :@"iPad 4",
@"iPad4,1" :@"iPad Air", // 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" :@"iPad Air", // 5th Generation iPad (iPad Air) - Cellular
@"iPad5,3" :@"iPad Air2", //
@"iPad5,4" :@"iPad Air2", //
@"iPad2,5" :@"iPad mini 1G",
@"iPad2,6" :@"iPad mini 1G",
@"iPad2,7" :@"iPad mini 1G",
@"iPad4,4" :@"iPad mini 2",
@"iPad4,5" :@"iPad mini 2",
@"iPad4,6" :@"iPad mini 2",
@"iPad4,7" :@"iPad mini 3",
@"iPad4,8" :@"iPad mini 3",
@"iPad4,9" :@"iPad mini 3",
@"AppleTV2,1" : @"Apple TV 2G",
@"AppleTV3,1" : @"Apple TV 3G",
@"AppleTV3,2" : @"Apple TV 3G",
};
}
NSString* deviceName = [deviceNamesByCode objectForKey:code];
if (!deviceName) {
// Not found on database. At least guess main device type from string contents:
if ([deviceName rangeOfString:@"iPod"].location != NSNotFound) {
deviceName = @"iPod Touch";
}
else if([deviceName rangeOfString:@"iPad"].location != NSNotFound) {
deviceName = @"iPad";
}
else if([deviceName rangeOfString:@"iPhone"].location != NSNotFound){
deviceName = @"iPhone";
}
else if ([deviceName rangeOfString:@"AppleTV"].location != NSNotFound) {
deviceName = @"AppleTV";
} else if ([deviceName rangeOfString:@"86"].location != NSNotFound) {
deviceName = @"Simulator";
}
}
return deviceName;
}
参考:http://stackoverflow.com/questions/11197509/ios-iphone-get-device-model-and-make
系统版本判断:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
参考:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
swift查看底层:
swift -dump-ast swiftname
/Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -dump-ast a.swift
打印当前window的层次:
po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
- iOS5及以上版本layout放在:
viewWillLayoutSubviews:
或
viewDidLayoutSubviews:
- iOS4系统layout放在:
layoutSubviews
计算文本区域:
+ (CGSize)sizeOfString:(NSString *)string font:(UIFont *)font size:(CGSize)size
{
CGSize stringSize = CGSizeZero;
if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
stringSize = [string sizeWithFont:font
constrainedToSize:size
lineBreakMode:UILineBreakModeWordWrap];
} else {
stringSize = [string boundingRectWithSize:size
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:font.fontDescriptor.fontAttributes
context:nil].size;
}
return stringSize;
}
更改UIToolBar背景色
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
toolBar.translucent = NO;
toolBar.barTintColor = [UIColor whiteColor];
}
四舍五入保留两位小数
float a = 3.456; //保留到小数点后两位
float b =(int)((a * 100) + 0.5) / 100.0;
//output b = 3.46;
不同view继承体系之间区域转换
例如一个视图控制器的view中有一个UITableView,UITableView的某个cell中有个UITextField,想要得到UITextField在view中的位置:
CGRect rect = [self.view convertRect:textField.frame fromView:textField.superview];
//或者
CGRect rect = [textField.superview convertRect:textField.frame toView:self.view];
不同view继承体系之间点转换
- 把一个点从一个坐标系转换到接收者的坐标系
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
参数
point
一个视图中坐标系上的点
view
一个视图包含了点和他自身坐标系。如果是图是nil,那么这个方法将尝试转换基于窗口的坐标系。否则视图和那个接收者必须属于同一个UIWindow对象。
- 转换一个点从接收者坐标系到给定的视图坐标系
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
参数
point
一个在调用者坐标系中的点
view
一个包含了需要被转换的点的视图。如果视图是nil,那么这个方法将会转换成基于窗口的坐标。否则视图和接收者都要属于同一个UIWindow对象。
返回值
Block 使用方式
How Do I Declare A Block in Objective-C?
- As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
- As a property:
@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
- As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
- As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
- As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
打印当前调用处堆栈信息: