今天做了一个上海的天气预报,正好把前些日子所学用起来,但是我发现学的不扎实啊,
这个是头文件中的
#import <UIKit/UIKit.h>
@interface YUViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> { NSArray *pickerViewData;//pickerView的数据 } - (IBAction)waction:(id)sender; @property (weak, nonatomic) IBOutlet UILabel *wlabel; - (IBAction)cityAction:(id)sender; @property (weak, nonatomic) IBOutlet UILabel *cityLabel; @property (weak, nonatomic) IBOutlet UIPickerView *cityPicture;@end
下面是实现文件内容
还要在添加的空间中pickerview设置数据源和delegate为file's owner
#import "YUViewController.h"@interface YUViewController ()@end@implementation YUViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. pickerViewData = [NSArray arrayWithObjects:@"上海:101020100.html",@"宝山:101020300.html",@"嘉定:101020500.html",@"南汇:101020600.html",@"浦东:101021300.html",@"青浦:101020800.html",@"松江:101020900.html",@"奉贤:101021000.html",@"崇明:101021100.html",@"徐家汇:101021200.html",@"闵行:101020200.html",@"金山:101020700.html", nil];}- (IBAction)waction:(id)sender{ //----------------这个是什么东西----------------// NSInteger row=[_cityPicture selectedRowInComponent:0];//nsinterger不是指针 NSString *city=[pickerViewData objectAtIndex:row]; //截取字符index为3 到结尾之间的内容(0,1,2,)//徐家汇这个需要重新设置 NSString *lastcity=[city substringFromIndex:3]; //_cityLabel.text=lastcity; NSError *error; NSString *http=@"http://m.weather.com.cn/data/"; NSURL *url=[[NSURL alloc]initWithString:[http stringByAppendingString:lastcity]]; NSLog(@"%@",url); NSURLRequest *request = [NSURLRequest requestWithURL:url];// //加载一个NSURL对象// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101020300.html"]]; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; NSString *text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; NSLog(@"weatherInfo:%@", text ); _wlabel.text=text;}//实现如下方法,设置Picker View视图中的组件(列)数量:-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}//实现如下方法,设置Picker View视图中显示的项(行)数:-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return [pickerViewData count];}//实现如下方法,将pickerviewData数据填充到Picker View中:-(UIView *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [pickerViewData objectAtIndex:row];}- (IBAction)cityAction:(id)sender{ NSInteger row=[_cityPicture selectedRowInComponent:0];//nsinterger不是指针 NSString *city=[pickerViewData objectAtIndex:row]; //截取字符index为3 到结尾之间的内容(0,1,2,)//徐家汇这个需要重新设置 NSString *lastcity=[city substringFromIndex:3]; _cityLabel.text=lastcity;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}等会儿更新修改其中的瑕疵;
Objective-C语法之字符串那些事
雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客,原文地址:
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
1.创建经典的NSString字符串
NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组。 @+ " 字符串 " 这个符号为objective-c NSString 字符串常量的标准用法,char* 创建的时候 无需添加@
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //经典的字符串赋值
- NSString *str0 = @"我是雨松MOMO";
- //字符串格式化合并分别包括
- //NSString*类型 int类型 char*类型
- NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"雨松MOMO", 25,"xuanyusong@gmail.com"];
- //字符串赋值 参数中只可以写一个字符串 和第一种很像
- NSString *str2 = [NSString stringWithString:@"我是字符串"];
- //字符串转换为utf-8格式 参数为char*类型
- NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"];
- //字符串合并
- int i = 100;
- char*c = "xuanyusong";
- NSString *temp = @"我是临时字符串";
- //在字符串temp的基础继续添加 int i 与 char* c 组成一个新的字符串
- NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c];
- //在字符串temp的基础继续添加temp 并组成一个新的字符串
- NSString *str5 = [temp stringByAppendingString:temp];
- //字符串输出
- NSLog(@"str0 = %@", str0);
- NSLog(@"str1 = %@", str1);
- NSLog(@"str2 = %@", str2);
- NSLog(@"str3 = %@", str3);
- NSLog(@"str4 = %@", str4);
- NSLog(@"str5 = %@", str5);
- }
2.字符串的遍历
每一个字符串其实是由若干个char字符组成,字符串的遍历实际上就是将字符串中的每一个字符提取出来。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //经典的字符串赋值
- NSString *str = @"YUSONGMOMO";
- //字符串的长度
- int count = [str length];
- NSLog(@"字符串的长度是%d",count);
- //遍历字符串中的每一个字符
- for(int i =0; i < count; i++)
- {
- char c = [str characterAtIndex:i];
- NSLog(@"字符串第 %d 位为 %c",i,c);
- }
- }
3.字符串的比较
isEqualToString 比较字符串是否完全相等,大小写不一样也无法完全匹配。
hasPrefixe 匹配字符串头
haSuffix 匹配字符串的尾巴
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSString *str0 = @"雨松MOMO";
- NSString *str1 = @"雨松MOMO";
- //字符串完全相等比较
- if([str0 isEqualToString:str1])
- {
- NSLog(@"字符串完全相等");
- }
- //字符串以开头比较
- if([str0 hasPrefix:@"雨松"])
- {
- NSLog(@"字符串str0以雨松开头");
- }
- //字符串以结尾比较
- if([str1 hasSuffix:@"MOMO"])
- {
- NSLog(@"str1字符串以MOMO结尾");
- }
- }
4.字符串的截取与大小写改变
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSString *str0 = @"中文my name is xuanyusong";
- //截取字符串起始点到index为4之间的内容
- NSString * to = [str0 substringToIndex:4];
- NSLog(@"to = %@",to);
- //截取字符index为2 到结尾之间的内容
- NSString * from = [str0 substringFromIndex:2];
- NSLog(@"from = %@",from);
- //设置截取字符串的范围
- //从第二位到第十位
- NSRange rang = NSMakeRange(2, 10);
- NSString * strRang = [str0 substringWithRange:rang];
- NSLog(@"rang = %@",strRang);
- //设置字符串首字母大写
- NSLog(@"str0首字母大写:%@",[str0 capitalizedString]);
- //设置字符串全部内容为大写
- NSLog(@"str0大写:%@",[str0 uppercaseString]);
- //设置字符全部内容为小写
- NSLog(@"str0小写:%@",[str0 lowercaseString]);
- }
5.搜索字符串与替换字符串
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSString *str0 = @"中文my name is xuanyusong";
- NSString *temp = @"is";
- NSRange rang = [str0 rangeOfString:temp];
- NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location);
- NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length);
- //将搜索中的字符串替换成为一个新的字符串
- NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"];
- NSLog(@"替换后字符串为%@", str);
- //将字符串中" " 全部替换成 *
- str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"];
- NSLog(@"替换后字符串为%@", str);
- }
拓展:使用下面这个方法整体替换字符串还可以设置替换的区域。
ons:<#(NSStringCompareOptions)#> range:<#(NSRange)#>
stringByReplacingOccurrencesOfString:(NSString *)withString:(NSString *) options:(NSStringCompareOptions) range:(NSRange)
这一篇主要介绍NSString 类的使用 下面篇详细介绍NSMutableString类,大小盆友们要支持MOMO喔 嘻嘻~~~
上一篇: 下一篇:
参考地址http://blog.csdn.net/xys289187120/article/details/6777283