您的位置:首页 > 移动开发 > IOS开发

iOS 开发问题集锦(三)

2013-08-12 11:24 369 查看

iOS 开发问题集锦(三)

介于群里大部分童鞋都是新手,为了大家能够更好的提问,并且提的问题能更好的得到回答,下面写几点提问时的注意事项:

1、认真对待你的问题,在提问题前有过认真的思考;

2、先在 google 搜索,一般的问题都可以找到答案,在天朝google经常会抽抽,这个时候需要大家学会怎么去FQ;

3、问题具体化,让你的问题处于具体的环境中,把问题阐述清楚,避免大而空洞、需要具体情况来分析、或别人难以读懂的问题;

4、话不在多,尽量提供精确的信息,把信息裁剪的越小越好。这样做有几点好处:首先表明你对你的提问付出了努力,可以增加回答的机会;其次,问题简化后可以使有用答案出现的几率增加;再次,在你提炼问题的时候,也许你自己就能找出问题的所在,然后做出修改;

5、明白你想问的是什么,能给你有用答案的人也正是最忙的人;

6、别用无意义的话语结束提问,例如:“有人能帮我吗?”、“有人在吗?”或者“有答案吗?”之类的话。这样问就是画蛇添足,别人会很厌烦你;

7、尽量别出现错别字,不然看不懂。

希望以上几点能够帮到大家。

下面是此次的问题集锦。

1、怎么分解url字符串,只要问号前边的字符?或各个参数?如下字符串:

NSString *url = @"http://hzd.exi.hotchaleur.com/mp3_64_60/06/fc/06d7480aadbbcbb8bb7cee6f136c05fc.mp3?k=f2a52712bc03c5c3&t=1376447585";


a、以 "?" 来分割为字符串数组,然后取索引为 0 的字符

[url componentsSeparatedByString:@"?"][0]


b、把这个字符转换为 NSURL类型,然后拼接字符串

NSURL *query = [NSURL URLWithString:url];
NSLog(@"url -> http://%@%@", query.host, query.path);


c、写个独立的方法,用字典的方式显示url字符串中的各个参数,此方法只针对正确地url(错误的url请忽略...)

/**
解析查询字符串
query : 查询字符串,以 ‘&’ 分隔
*/
+ (NSDictionary *)parseQueryString:(NSString *)query {
// 定义字典
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

// 检测字符串中是否包含 ‘?’
NSRange range = [query rangeOfString:@"?"];
if(range.location != NSNotFound){
NSArray *queryArr = [query componentsSeparatedByString:@"?"];
[dict setObject:queryArr[0] forKey:@"url"];
query = queryArr[1];
}else{
// 如果一个url连 '?' 都没有,那么肯定就没有参数
return dict;
}

// 检测字符串中是否包含 ‘&’
if([query rangeOfString:@"&"].location != NSNotFound){
// 以 & 来分割字符,并放入数组中
NSArray *pairs = [query componentsSeparatedByString:@"&"];
// 遍历字符数组
for (NSString *pair in pairs) {
// 以等号来分割字符
NSArray *elements = [pair componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 添加到字典中
[dict setObject:val forKey:key];
}
}else if([query rangeOfString:@"="].location != NSNotFound){ // 检测字符串中是否包含 ‘=’
NSArray *elements = [query componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 添加到字典中
[dict setObject:val forKey:key];
}

NSLog(@"dict -> %@", dict);
return dict;
}


2、设置App启动页,如下图:



启动页图片分为3中像素:320*480、640*960、640*1136。

首先拖动图片到上图3中相应的位置,然后在工程的AppDelegate.m中的下列方法中设置:

/*
说明:当程序载入后执行
**/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//延时3秒,以便用户看清楚启动页
[NSThread sleepForTimeInterval:3.0];

// 添加启动页
UIImageView *splashScreen = [[UIImageView alloc] initWithFrame:self.window.bounds];
if (iPhone5) {
splashScreen.image = [UIImage imageNamed:@"Default-568h"];
}else{
splashScreen.image = [UIImage imageNamed:@"Default"];
}
[self.window addSubview:splashScreen];

// 启动页渐变效果
[UIView animateWithDuration:0.5 animations:^{
CATransform3D transform = CATransform3DMakeScale(1.0, 1.0, 1.0);
splashScreen.layer.transform = transform;
splashScreen.alpha = 0.0;
} completion:^(BOOL finished) {
[splashScreen removeFromSuperview];
}];

[self.window makeKeyAndVisible];

return YES;
}


3、点击背景视图,收起输入法。

在 - (void)viewDidLoad 中添加如下代码:

// 设置 self.view GestureRecognizer
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleBackgroundTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];


然后,编写 handleBackgroundTap: 方法

/*
点击背景时关闭键盘
**/
-(void)handleBackgroundTap:(UITapGestureRecognizer *)sender{
[self.userName resignFirstResponder];
[self.userPassword resignFirstResponder];
}


4、删掉字符串中的包含的html标签,以及转义符

/**
删掉字符串中的包含的html标签,以及转义符
strHtml : 含有html标签的字符串
*/
+ (NSString *)stringByStrippingHTML:(NSString *)strHtml{
NSRange r;
NSString *s = [strHtml copy];
while ((r = [s rangeOfString:@"<[^>]+>|&[^;]+;" options:NSRegularExpressionSearch]).location != NSNotFound){
s = [s stringByReplacingCharactersInRange:r withString:@""];
}
return s;
}


5、获得今天为星期几

/**
获得今天为星期几
*/
+ (NSInteger)getweek{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

comps = [calendar components:unitFlags fromDate:[NSDate date]];
return [comps weekday] - 1;
}


著作权声明:本文由 http://wzrong.cnblogs.com 或者 http://iostour.diandian.com 原创,欢迎转载分享。 请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

原创文章,如需转载请注明出处,谢谢!

欢迎访问本人技术微博 @iOS之旅 相互交流,共同学习,共同进步!

欢迎访问本人微博 @卫志荣
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: