开发者

How to add a string into an NSArray at particulap index?

开发者 https://www.devze.com 2023-01-13 14:41 出处:网络
NSInteger timeNum = time; NSMutableArray theResultArray = [[NSMutableArray alloc] init]; [theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex];
NSInteger timeNum = time; 
NSMutableArray theResultArray = [[NSMutableArray alloc] init];
[theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex]; 

This is the way we开发者_如何学Go do to insert NSInteger to array. How to i do it for NSString?


It's easier because you don't have to encapsulate the integer:

[theResultArray insertObject:myNSString atIndex:rightIndex];

Hope that helps.


It's exactly the same:

NSString* myNSString = @"foo"; 
NSMutableArray theResultArray = [[NSMutableArray alloc] init];
[theResultArray insertObject:myNSString atIndex:rightIndex];

Be aware that if you try to insert an object in a position that doesn't exist (i.e. if rightIndex > [theResultArray count]), you will get an exception. In the example above, the only legal value for rightIndex is 0 because the array is empty.

0

精彩评论

暂无评论...
验证码 换一张
取 消