zoukankan      html  css  js  c++  java
  • 关于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)
     

  • 相关阅读:
    装饰器实例
    生成器、迭代器脚本实例
    魔法方法和属性
    随机生成验证码
    认证客户端的链接合法性
    将socket通信实现多进程
    线程锁模拟抢票系统
    ntp时间服务器
    蛇形串---------
    两年内计划
  • 原文地址:https://www.cnblogs.com/lovecode/p/2283914.html
Copyright © 2011-2022 走看看