IOS开发的一些技巧和常见问题
2012-05-08 11:38
405 查看
1.什么时候该用ASIHTTPRequest,什么时候该用ASIFormDataRequest
如果是 Get ,或者参数可以简单的组成 Get 方式提交的时候, 比如 thread.php?id=123 , 就用 ASIHTTPRequest 就可以了原因是简单
如果必须是 POST ,或者参数很多,你要构造一个带参数的 URL 很麻烦的时候,就用 ASIHttpFormDataRequest
2.三个比较经典的git的库,关于状态栏的。
https://github.com/myell0w/MTStatusBarOverlay/
https://github.com/enormego/PhotoViewer
https://github.com/mwaterfall/MWPhotoBrowser
3.验证邮箱的合法性(转http://blog.csdn.net/pjk1129/article/details/6652636)
4.timer使用注意 在使用uiscrollView划动时停止
问题描述:
用一个NSTimer定时刷新,而在UISCrollView划动发生的过程中,刷新的效果停止.
解决办法:
需要注意的:
timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
在设置为target时,其retaincount 加一.!!!!
一定注意
2.NSClassFromString 和 NSSelectorFromString
NSClassFromString
NSSelectorFromString
正常来说,
id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];
和
id myObj = [[MySpecialClass alloc] init];
是一样的。但是,如果你的程序中并不存在MySpecialClass这个类,下面的写法会出错,而上面的写法只是返回一个空对象而已。
因此,在某些情况下,可以使用NSClassFromString来进行你不确定的类的初始化。
比如在iPhone中,NSTask可能就会出现这种情况,所以在你需要使用NSTask时,最好使用:
[[NSClassFromString(@"NSTask") .....]]
而不要直接使用[NSTask ...]这种写法。
NSClassFromString的好处是:
1 弱化连接,因此并不会把没有的Framework也link到程序中。
2 不需要使用import,因为类是动态加载的,只要存在就可以加载。
for (int c=0; c<[classNames count]; c++) {
NSString*className=[classNames objectAtIndex:c];
id class=[[NSClassFromString(className) alloc] init];
for (int i=0; i<[params count]; i++) {
[class performSelector:NSSelectorFromString([NSStringstringWithFormat:@"setA%i",i])];
}
}
如果是 Get ,或者参数可以简单的组成 Get 方式提交的时候, 比如 thread.php?id=123 , 就用 ASIHTTPRequest 就可以了原因是简单
如果必须是 POST ,或者参数很多,你要构造一个带参数的 URL 很麻烦的时候,就用 ASIHttpFormDataRequest
2.三个比较经典的git的库,关于状态栏的。
https://github.com/myell0w/MTStatusBarOverlay/
https://github.com/enormego/PhotoViewer
https://github.com/mwaterfall/MWPhotoBrowser
3.验证邮箱的合法性(转http://blog.csdn.net/pjk1129/article/details/6652636)
//通过区分字符串 -(BOOL)validateEmail:(NSString*)email { if((0 != [email rangeOfString:@"@"].length) && (0 != [email rangeOfString:@"."].length)) { NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease]; [tmpInvalidMutableCharSet removeCharactersInString:@"_-"]; NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch]; //取得用户名部分 NSString* userNameString = [email substringToIndex:range1.location]; NSArray* userNameArray = [userNameString componentsSeparatedByString:@"."]; for(NSString* string in userNameArray) { NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet]; if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""]) return NO; } //取得域名部分 NSString *domainString = [email substringFromIndex:range1.location+1]; NSArray *domainArray = [domainString componentsSeparatedByString:@"."]; for(NSString *string in domainArray) { NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet]; if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""]) return NO; } return YES; } else { return NO; } } //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:email]; }
4.timer使用注意 在使用uiscrollView划动时停止
问题描述:
用一个NSTimer定时刷新,而在UISCrollView划动发生的过程中,刷新的效果停止.
解决办法:
- (void)setTimer { NSTimer *timer; NSDate *date = [NSDate date]; timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; self.m_timer = timer; [timer release]; } //在NSDefaultRunLoopMode模式下,没有更新
需要注意的:
timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
在设置为target时,其retaincount 加一.!!!!
一定注意
2.NSClassFromString 和 NSSelectorFromString
NSClassFromString
NSSelectorFromString
正常来说,
id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];
和
id myObj = [[MySpecialClass alloc] init];
是一样的。但是,如果你的程序中并不存在MySpecialClass这个类,下面的写法会出错,而上面的写法只是返回一个空对象而已。
因此,在某些情况下,可以使用NSClassFromString来进行你不确定的类的初始化。
比如在iPhone中,NSTask可能就会出现这种情况,所以在你需要使用NSTask时,最好使用:
[[NSClassFromString(@"NSTask") .....]]
而不要直接使用[NSTask ...]这种写法。
NSClassFromString的好处是:
1 弱化连接,因此并不会把没有的Framework也link到程序中。
2 不需要使用import,因为类是动态加载的,只要存在就可以加载。
for (int c=0; c<[classNames count]; c++) {
NSString*className=[classNames objectAtIndex:c];
id class=[[NSClassFromString(className) alloc] init];
for (int i=0; i<[params count]; i++) {
[class performSelector:NSSelectorFromString([NSStringstringWithFormat:@"setA%i",i])];
}
}
相关文章推荐
- IOS开发的一些技巧和常见问题
- IOS开发的一些技巧和常见问题
- iOS 开发中的一些小问题和技巧
- iOS 持续更新 开发中的一些小问题和技巧1
- 101-开发技巧和一些常见的问题(1)
- iOS开发的一些技巧整理
- ios开发---常见问题
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- 移动端web开发开发技巧和常见问题
- WEB前端开发人员须知的常见浏览器兼容问题及解决技巧
- 【汇总】iOS开发及Xcode使用中遇到的一些报错问题汇总
- iOS开发中两个常见问题的处理
- iOS开发Automatic Refernce Counting 下注意的一些问题
- iOS10开发需要注意的一些问题(转)
- iOS开发中遇到的一些问题以及解决办法总结
- iOS开发中的一些经验问题
- iOS 常见一些问题
- Mac/iOS下开发程序常见问题
- iOS实战开发常见问题(一)
- iOS 开发中一些不常用技巧