IOS - 常用开发Tips
(2014-01-21 20:50:42)
标签:
ios-常用开发tipsit |
分类: Mac/IOS那些事 |
1:ARC下NSNotificationCenter需要remove
- (void) dealloc
{
// [super dealloc] 千万不要画蛇添足
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
2:const or #define
下面是大家常见的两种定义优先选择第二种
#define PI 3.14159 //只预处理时进行符号替换,不推荐 const doulbe Pi=3.14159 //安全性高,和效率上推荐
3:常量字符串声明
Foo.h
extern NSString *constXXFooDidBarNotification;
Foo.m
NSString *const XXFooDidBarNotification =@"XXFooDidBarNotification”;
上面是引用的是
有些童鞋不太理解const 修饰的用法 尝尝写成
constNSString *XXFooDidBarNotification =@"XXFooDidBarNotification”;
详情可以参看 :http://walkingsmarts.com/correct-way-of-defining-constants-in-objective-c/
4:NSTimer invalidate 错误
之前一直用NSTimer ,这两天再做性能优化的时候 发现了问题,用到NSTimer的ViewControll 一直没有释放. 后来跟踪发现了问题.
[scheduledTimerWithTimeInterval :target:selector:userInfo:repeats:]
当target 为self (VC)的时候 因为Timer 强引用了self,
所以当你的
[ timer invalidate ]
写到 dealloc 的时候 是永远无法释放当前VC的
5:NSTimer并不是一个高精度的定时器
苹果官方文档已经给出了
>
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
所以当你需要高精度的定时器的时候
更正:https://developer.apple.com/library/ios/technotes/tn2169/_index.html
6:调试的技巧
有没有用过下面的log输出呢尝试下吧
-(void) buttonPressed :(UIButton *)button
{
NSLog(@"%s", __PRETTY_FUNCTION__);NSLog(@"Stack trace: %@", [NSThread callStackSymbols]); NSLog(@"Current selector: %@", NSStringFromSelector(_cmd)); NSLog(@"Object class: %@", NSStringFromClass([self class])); NSLog(@"Filename: %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent]); ...
}
7:注释技巧
- 在代码中加注释时,如果以 // TODO: 或 // FIXME: 或者 // !!!: 或 // ???: 开头的话,此注释会出现在方法的下列列表里,方便日后跟踪。
- 利用Xcode4中的自定义代码片段,可以用一个快捷键插入一个预先定义好的注释片段
8:改变模拟器窗口的大小
在分辨率比较低的时候,iPad和iPhone模拟器会自动缩小,如果希望保持原始大小,可以修改 /Developer/Platforms/iPhoneSimulator.platform/Developer/Application/iOS Simulator/Contents/Info.plist,添加一个Application UI Presentaion Mode,设成All surpressed,则运行模拟器时自动隐藏菜单条和dock。在此目录下的 Resources/Devices/iPad.deviceinfo/Info.plist 中的chromeImageFile,把值设成空,可以去掉模拟器的边框。
9:enum or NS_ENUM
这是昨天晚上在slack内讨论的话题有必要加进来正好之前唐巧微博分享过的有兴趣的可以去
https://github.com/raywenderlich/objective-c-style-guide
12 判断字符串里面是否有 某个字符
13 刷新 uitableView 的某一个cell
NSIndexPath *reloadIndex = [NSIndexPath indexPathForRow:0 inSection:0];
14.修改uicolor的填充值
直接使用16进制
+ (UIColor *) colorWithHexString: (NSString *) stringToConvert
{
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [stringToConvert substringWithRange:range];
range.location = 2;
NSString *gString = [stringToConvert substringWithRange:range];
range.location = 4;
NSString *bString = [stringToConvert substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
blue:((float) b / 255.0f)
}
self.accordion.accordionSectionColor = [CommonHelper colorWithHexString:@"ebf1f7];
用宏的方式来使用RGB
#define UIColorFromRGB(redValue,greenValue,blueValue) [UIColor colorWithRed:(redValue)/255.0 green:(greenValue)/255.0 blue:(blueValue)/255.0 alpha:1.0]
self.view.backgroundColor = UIColorFromRGB(154, 189, 240);
用宏类定义16进制颜色的写法
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
self.selectedTextColor = UIColorFromRGB(0xF2F2F2);
15 弹出最高优先级的UIview 要拿到 keyWindow
16当自定义滑动删除的时候,ios7,6 会默认出来 系统默认的删除 用这句来屏蔽掉默认的,
-(UITableViewCellEditingSt
{
}
17 弹出到 nav上面的 uiview
18 弹出层覆盖状态栏 的时候需要uiview
如此添加
[[UIApplication sharedApplication].keyWindow addSubview:self.poptextView];
18
|
1
2
3
4
5
6
7
8
|
NSArray
*array = [NSArray
arrayWithObjects:@"1",@"3",@"5",@"10",
nil];
Byte
byte[[array
count]];
for(int
i=0;i<</code>
byte[i]
= [[array
objectAtIndex:i]
intValue];
}
NSData
*d = [[NSData
alloc]
initWithBytes:&array;
length:4]; |

加载中…