如何将字符串添加到 NSArray 的特定索引处?

发布于 2024-09-16 12:18:58 字数 253 浏览 4 评论 0原文

NSInteger timeNum = time; 
NSMutableArray theResultArray = [[NSMutableArray alloc] init];
[theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex]; 

这就是我们将 NSInteger 插入数组的方法。我该如何为 NSString 做到这一点?

NSInteger timeNum = time; 
NSMutableArray theResultArray = [[NSMutableArray alloc] init];
[theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex]; 

This is the way we do to insert NSInteger to array. How to i do it for NSString?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-09-23 12:18:58

这更容易,因为您不必封装整数:

[theResultArray insertObject:myNSString atIndex:rightIndex];

希望有帮助。

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

[theResultArray insertObject:myNSString atIndex:rightIndex];

Hope that helps.

旧伤还要旧人安 2024-09-23 12:18:58

完全相同:

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

请注意,如果您尝试在不存在的位置插入对象(即,如果 rightIndex > [theResultArray count]),您将收到异常。在上面的示例中,rightIndex 的唯一合法值为 0,因为数组为空。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文