如何将 NSMutableString 中的数据添加到 NSArray 中?

发布于 2024-08-17 08:01:35 字数 52 浏览 9 评论 0原文

是否可以将 NSMutableString 中的值添加到 NSArray 中?片段是什么?

is it an possible to add a value from an NSMutableString into an NSArray? Whats the snippet?

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

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

发布评论

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

评论(5

夜还是长夜 2024-08-24 08:01:35

事实上,迈克错了。如果你想用单个 NSMutableString 对象实例化一个 NSArray,你可以执行以下操作:

NSMutableString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObject:myString];

NSArray 中没有 arrayWithElements (参见 NSArray 文档

Actually, Mike is wrong. If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:

NSMutableString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObject:myString];

There is no arrayWithElements in NSArray (see NSArray documentation)

私藏温柔 2024-08-24 08:01:35

如果您想使用单个 NSMutableString 对象实例化一个 NSArray ,您可以执行以下操作:

NSString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObjects:myString,nil];

请注意,NSArray 将是不可变的 - 也就是说,您无法添加或删除完成后反对它。如果您希望数组是可变的,则必须创建一个 NSMutableArray。要以这种方式使用 NSMutableArray,您可以执行以下操作:

NSString *myString; //Assuming your string is here
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:myString];

If you want to instantiate an NSArray with a single NSMutableString object, you can do the following:

NSString *myString; //Assuming your string is here
NSArray *array = [NSArray arrayWithObjects:myString,nil];

Note that NSArray will be immutable - that is, you can't add or remove objects to it after you've made it. If you want the array to be mutable, you'll have to create an NSMutableArray. To use an NSMutableArray in this fashion, you can do the following:

NSString *myString; //Assuming your string is here
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:myString];
多彩岁月 2024-08-24 08:01:35

NSArray 是不可变的,所以你不能给它添加值。您应该使用 NSMutableArray 以便使用 addObject: 方法执行此操作。

NSMutableString *str = ...
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:str];

NSArray is immutable, so you cannot add values to it. You should use NSMutableArray in order to do that with the addObject: method.

NSMutableString *str = ...
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:str];
站稳脚跟 2024-08-24 08:01:35
// You must use NSMutableArray to add Object to array

NSMutableArray *tableCellNames;
// arrayWithCapacity is a required parameter to define limit of your object.
tableCellNames = [NSMutableArray arrayWithCapacity:total_rows];

[tableCellNames addObject:title];
NSLog(@"Array table cell %@",tableCellNames);

//Thanks VKJ
// You must use NSMutableArray to add Object to array

NSMutableArray *tableCellNames;
// arrayWithCapacity is a required parameter to define limit of your object.
tableCellNames = [NSMutableArray arrayWithCapacity:total_rows];

[tableCellNames addObject:title];
NSLog(@"Array table cell %@",tableCellNames);

//Thanks VKJ
腻橙味 2024-08-24 08:01:35

一个优雅的解决方案是这样的:

NSMutableString *str; //your string here
NSArray *newArray = @[str];

使用新的符号,这是小菜一碟。

An elegant solution would be this:

NSMutableString *str; //your string here
NSArray *newArray = @[str];

Using the new notation, it's a piece of cake.

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