最近PM2.5很火爆,闲着无事自己做个看看。下面是界面~
数据拿的是北京市环境保护监测中心的(http://zx.bjmemc.com.cn/)
只有弱智的查看功能~加上了自动获取上次关闭前浏览的地区,方便下次打开之间就能显示。
学了点combox的用法,如获取选择的索引号:
- (void)comboBoxSelectionDidChange:(NSNotification *)notification { @try { selectIndex = [(NSComboBox *)[notification object] indexOfSelectedItem]; NSLog(@"you select %ld, %@", selectIndex,[stationInfo objectAtIndex:selectIndex]); } @catch (NSException *exception) { NSLog(@"%@", exception.description); } }
还有JSON的用法,用的是MacOS自带的NSJSONSerialization
stationInfo = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSDictionary *stationArray = [stationInfo objectAtIndex:i]; [stationName addObject:[stationArray objectForKey:@"StationName"]];
如图右下角的颜色显示,从数据中获取过来的是颜色的HEX值,需要转换为RGB才能在Objective-C中使用。
- (NSColor *)colorWithHexString:(NSString *) inColorString { NSColor *result = nil; unsigned int colorCode = 0; unsigned char redByte, greenByte, blueByte;//, alphaByte if (inColorString) { NSScanner *scanner = [NSScanner scannerWithString:inColorString]; (void) [scanner scanHexInt:&colorCode]; redByte = (unsigned char) (colorCode >> 16); greenByte = (unsigned char) (colorCode >> 8); blueByte = (unsigned char) (colorCode); result = [NSColor colorWithCalibratedRed:(float)redByte/0xff green:(float)greenByte/0xff blue:(float)blueByte/0xff alpha:1.0]; } return result; }