How to add string objects to NSMutableArray

前端 未结 6 1158
难免孤独
难免孤独 2021-01-01 10:06

I have an NSMutableArray named randomSelection:

NSMutableArray *randomSelection;

I am then trying to add strings to this array if certain c

相关标签:
6条回答
  • 2021-01-01 10:44

    Just allocate your NSMutableArray. You'll get solved your problem.

    0 讨论(0)
  • 2021-01-01 10:49

    Try this:

    NSMutableArray *randomSelection = [[NSMutableArray alloc]init]; 
    [randomSelection addObject:@"string1"];
    
    0 讨论(0)
  • 2021-01-01 10:54

    First allocate the array using following statement & then objects in it.

    NSMutableArray *randomSelection =  [[NSMutableArray alloc] init];
    [randomSelection addObject:[NSString stringWithFormat:@"String1"]];
    [randomSelection addObject:[NSString stringWithFormat:@"String2"]];
    NSLog(@"Array - %@", randomSelection);
    

    This will definitely solves your problem.

    0 讨论(0)
  • 2021-01-01 11:04

    I think you are missing to allocate the memory for array. So try this

    NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
    [randomSelection addObject:@"string1"];
    NSString *test = [randomSelection objectAtIndex:0];
    NSLog(test);
    
    0 讨论(0)
  • 2021-01-01 11:04
    NSMutableArray *randomSelection =  [[NSMutableArray alloc]init];
    [randomSelection addObject:@"string1"];
    

    You need to alloc it first.

    0 讨论(0)
  • 2021-01-01 11:07

    Swift :

    var randomSelection: [AnyObject] = [AnyObject]()
    randomSelection.append("string1")
    let test: String = randomSelection[0] as! String
    print(test)
    

    OR

    let array : NSMutableArray = []
    array.addObject("test String")
    print(array)
    
    0 讨论(0)
提交回复
热议问题