2016-10-07 6 views
-4

NSStringsで配列を作成する方法を知っている人なら、実際にはネットでどこでも良い例を見つけることができないので、これは役立ちますが、これは家事ではありません。それは私の先生が私に勉強してくれた質問のpdfにありますが、これを理解することはできません。 誰かが何らかの説明を書いた例を知っていれば、これで時間をとってくれてありがとう。NSStringsの配列を作成する方法は?

+1

をあなたのための難しい部分? – matt

答えて

0

あなたが何を求めているかは完全にはっきりしていません。あなただけの不変の配列を作成する必要がある場合は、オブジェクトリテラル構文を使用することができます。

NSArray* stringArray = @[@"one", @"two", @"three"]; 

あなたが変更することができる可変配列をしたい場合、あなたは可変配列を作成することができます何

//Create an empty mutable array 
    NSMutableArray* stringArray = [NSMutableArray new]; 

    //Loop from 0 to 2. 
    for (int index = 0; index < 3; index++) { 
    //Create the strings "one", "two", and "three", one at a time 
    NSString *aString = [formatter stringFromNumber: @(index+1)]; 

    //Add the new string at the next available index. Note that the 
    //index must either an existing index into the array or the next available index. 
    //if you try to index past the end of the array you will crash. 
    stringArray[index] = aString; 

    //You could use addObject instead: 
    //[stringArray addObject: aString]; 
    } 
    NSLog(@"%@", stringArray); 
関連する問題