##iOS开发之iOS9新特性1

2016-03-03  本文已影响121人  纳萨立克

iOS开发之iOS9新特性

1. 新增的修饰词

@property (nonatomic, strong, nonnull) NSArray *names;
@property (nonatomic, strong) NSArray * __nonnull names;

@property (nonatomic, strong, nullable) NSArray *names;
@property (nonatomic, strong) NSArray * __nullable names;

@property (null_resettable, nonatomic, strong) NSArray *names;
NS_ASSUME_NONNULL_BEGIN
@interface ViewController ()
@property (nonatomic, strong) NSArray *a1;
@property (nonatomic, strong) NSArray *a2;
@property (nonatomic, strong) NSArray *a3;
@property (nonatomic, strong) NSArray *a4;
@property (nonatomic, strong) NSArray *a5;
@end
NS_ASSUME_NONNULL_END
// a1 a2 a3 a4 a5在NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END之间,他们就默认的都加上了nonnull的修饰词,也就是说a1,a2,a3,a4,a5的setter 和 getter都不可以为nil


// 错误写法
@property (nonatomic, assign, nullable) int age;
-  如果没有写修饰词,默认情况下是setter和getter都是可以为nil的.

2. 泛型

 NSArray *tempArr = @[@"1",@"2",@"3",@"4"];
 NSUInteger length = [tempArr[1] length];

在iOS9之前还没有引入泛型的时候 [tempArr objectatIndex:1]的方法的时候返回值是id类型,这个时候我们就不能有点语法来调用一些方法.泪如tempArr[1]是个字符串,但是由于返回类型是id类型,我们就不能使用.length来返回长度而需要[tempArr[1] length],泛型就很好的解决了这样的问题.

    //声明一个存放字符串的数组
     NSArray<NSString *> *tempArr2 = @[@"1",@"2",@"3",@"4"];
    
   // [tempArr2 objectAtIndex:1];此时这个方法的返回值不在是id类型了,而是NSString类型

    [tempArr2 objectAtIndex:1].length;
    //Box.h  
    
#import <Foundation/Foundation.h>

@interface Box<ObjectType>: NSObject

-(void)add:(ObjectType)object;
-(ObjectType)getWithIndex:(NSInteger *)index;

@end

//再创建一个pen类和一个book类
//创建一个只能放笔的box
    Box<pen *> *PenBox = [[Box alloc]init];
 //创建一个只能放书的box
    Box<Book *> *BookBox  = [[Box alloc]init];

3. UIWindow和控制statusBar的状态和颜色

-(BOOL)prefersStatusBarHidden{

        return YES;

}

-(UIStatusBarStyle)preferredStatusBarStyle{

    return UIStatusBarStyleLightContent;
}
///AppDelegate.m
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.topWindow = [[UIWindow alloc]initWithFrame:application.statusBarFrame];
    
    self.topWindow.windowLevel = UIWindowLevelAlert;
    
    //iOS9之前可以不用设置rootViewController,iOS9之后一定要设置
    self.topWindow.rootViewController =  [[TopWindowViewController alloc]init];
    
    self.topWindow.backgroundColor = [UIColor clearColor];
    self.topWindow.hidden = NO;
    return YES;
}
    -(BOOL)prefersStatusBarHidden{
    
    return YES;
    
    }
    
    -(UIStatusBarStyle)preferredStatusBarStyle{
    
    
         return UIStatusBarStyleLightContent;
    }
上一篇 下一篇

猜你喜欢

热点阅读