ios - 将NSString添加到NSMutableString

标签 ios objective-c nsstring

我想添加多个NSStringsNSMutableString来构建字符串列表。

这是我的方法中的代码,该方法采用NSDictionary并从中获取值-然后将其存储在NSString中,然后将字符串附加到NSMutableString中。首次运行此方法时,它将起作用。但是,当我再次调用该方法时,它将替换NSMutableString中的最后一个字符串。

这是代码:

NSString *userId = [NSString stringWithFormat:@"%@,", parameters[@"userId"]];
self.alluserIds = [NSMutableString string];


[self.alluserIds appendString:userId];

谁能告诉我我在做什么错?

self.alluserIds被声明为strong

最佳答案

这个:

self.alluserIds = [NSMutableString string];

正在创建一个新的可变字符串。在您要使用可变字符串之前,应该执行一次此操作,直到您想要重新启动时才应该再次执行此操作(因为重新创建可变字符串会破坏您拥有的旧实例)。

关于ios - 将NSString添加到NSMutableString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22909145/

相关文章:

ios - 旋转的 UIActionsheet

ios - 如何将图像添加到 UITableViewCell 删除按钮

objective-c - NSPredicate 的结果作为字符串

cocoa - 从 NSString 中去除非字母数字字符

ios - 获取字符在 NSString 中的位置

ios - Flutter Flavors : Can't find Generated. xcconfig

ios - 从应用程序委托(delegate)访问顶 View Controller

ios - Objective C 为什么将字符串格式化为字符串

ios - 跟踪所有运行 block

ios - 如何在注释掉的代码中编写 ObjectiveC 选择器并允许 Xcode 交叉引用