[Foundation] 简写代码 string literal


objective-c的语法有时候很罗嗦,有一些结构有支援缩写

  • NSNumber

     [NSNumber numberWithInt:1];
     @1;
        
     [NSNumber numberWithFloat:0.1f];
     @0.1f;

     [NSNumber numberWithBool:YES];
     @YES;

  • NSString
<span style="font-size:14px;font-weight: normal;">     [NSString stringWithString:@"Hello"]
     @"Hello"</span>
  • NSArray
<span style="font-size:14px;font-weight: normal;">     [NSArray arrayWithObjects:@"test", nil];
     @[@"test"];</span>

  • NSMutableArray

   <span style="font-size:14px;font-weight: normal;">NSMutableArray* array = [NSMutableArray arrayWithObjects:@"test", nil];    
    NSMutableArray* array2 = [@[@"test"] mutableCopy];</span>

  • NSDictionary
<span style="font-size:14px;font-weight: normal;">     [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
     @{@"key":@"value"};</span>

  • NSMutableDictionary

    NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithObject:@"value" forKey:@"key"];
    NSMutableDictionary* dic2 = [@{@"key":@"value"}mutableCopy];





    版权声明:本文为Eve28原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。