Initialize arrays of NSString in objective-c

我与影子孤独终老i 提交于 2019-12-18 12:49:04

问题


What's the easiest / fastest way to initialize an array of NSStrings in objective-c ?


回答1:


NSArray *array = [NSArray arrayWithObjects:@"String1",@"String2",@"String3",nil];



回答2:


  • C way:

    NSString *s1, *s2;
    NSString *cArray[]={s1, s2};
    

    The size of the cArray is 2, defined by the compiler.
    More info here: How to initialize all members of an array to the same value?

  • NSArray (Objective-C) way:

    NSArray *objCArray = [NSArray arrayWithObjects:@"1", @"2", nil];
    
  • If you are using XCode4 (LLVM4.0 compiler), now you can use NSArray literals:

    NSArray *array = @[ @"1", @"2" ];
    

    More info here: What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?




回答3:


NSArray *array = @[@"foo",@"bar"];




回答4:


NSString *stringArray[2] = {@"1", @"2"};


来源:https://stackoverflow.com/questions/5900123/initialize-arrays-of-nsstring-in-objective-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!