关于31天App教程示例中一些因SDK版本而出现的问题(转)

由于国外那个知名的31天案例教程比较老,所用官方API是2008年时的2.X,所以在现在的Xcode3-4之后或多或少都有编译警告和错误信息。必须做些适应iOS版本的代码更改才能顺利编译通过。

Day1:Minutes to Midnight

NSDate* now = [NSDate date];
int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];

这里dateWithCalendarFormat:在官方API 2.2.1以后,就会有警告错误。在Xcode3.X及其以后版本中,都要进行如下修改才能编译通过:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
int hour = 23 - [comps hour];
int min = 59 - [comps minute];
int sec = 59 - [comps second];

Day3: openURL
语句:NSLog(urlText); 产生警告错误: format string is not a string literal (potentially insecure)
修改为:NSLog(@"%@",urlText);
当然这句不变,NSString *urlText = [NSString stringWithFormat:@"http://maps.google.com.hk/maps?q=%@",addressText];

Day4: What is My IP
对于获取iphone本机ip地址,在iphone ios2.X上都可以用示例代码中的方法调用获得:

NSHost  *myhost = [NSHost  currentHost];

NSString *ad = [myhost address];

但是到ios 3.0后这些方法成了苹果私有API了,在编译时会报找不到此方法声明的错误,使用私有API其提交的app也要被reject。因此,建议使用网络上的 一个自定义获取ip地址的类文件。相关文件是:IPAddress.h (http://iphonesdksnippets.com/post/2009/09/07/Get-IP-address-of-iPhone.aspx)

转载:http://www.cnblogs.com/lovecode/archive/2011/12/11/2283914.html

posted on 2014-09-13 23:57  千兵卫博士  阅读(192)  评论(0编辑  收藏  举报

导航